Unique key fix

This commit is contained in:
Imbus 2023-12-15 03:50:23 +01:00
parent 53927b7cc5
commit 39833cabf2

View file

@ -6,48 +6,45 @@ import { style } from "../util/style";
import { SafeAreaView } from "react-native";
import { VirtualizedList } from "react-native";
const WINDOW_SIZE = 20;
export function PostsContainer({ navigation }): JSX.Element {
const [postData, setPostData] = React.useState<Post[]>([]);
const [page, setPage] = React.useState<number>(0);
const [offset, setOffset] = React.useState<number>(0);
const [refreshing, setRefreshing] = React.useState<boolean>(false);
const handleEndReached = (): void => {
setPage((prevPage) => prevPage + 1);
setOffset(offset + WINDOW_SIZE);
fetchMorePosts();
};
function onRefresh(): void {
refreshPosts();
}
React.useEffect(() => {
refreshPosts();
}, []);
React.useEffect(() => {
fetchMorePosts();
}, [page]);
// Full refresh
function refreshPosts(): void {
async function fetchPosts(): Promise<void> {
setPage(0);
setPostData(await getPostsInterval(0, 10));
}
setRefreshing(true);
setPostData([]);
setOffset(WINDOW_SIZE);
async function fetchPosts(): Promise<void> {
setPostData(await getPostsInterval(0, WINDOW_SIZE));
}
fetchPosts();
setRefreshing(false);
}
// Incremental fetch, should prune old posts
function fetchMorePosts(): void {
async function fetchPosts(): Promise<void> {
const newPosts = await getPostsInterval(page * 10, 10);
setPostData((oldPosts) => [...oldPosts, ...newPosts]);
setPostData([...postData, ...(await getPostsInterval(offset, WINDOW_SIZE))]);
}
fetchPosts();
}
function onRefresh(): void {
setRefreshing(true);
refreshPosts();
setRefreshing(false);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const getItemCount = (_data: unknown): number => postData.length;
@ -60,10 +57,8 @@ export function PostsContainer({ navigation }): JSX.Element {
<VirtualizedList
data={postData}
initialNumToRender={4}
renderItem={({ item }) => (
<PostView key={item.id + item.createdAt} post={item} />
)}
keyExtractor={(item: Post) => item.id + item.createdAt}
renderItem={({ item }) => <PostView key={item.id} post={item} />}
keyExtractor={(item: Post) => item.id}
getItemCount={getItemCount}
getItem={getItem}
onEndReached={handleEndReached}
@ -88,7 +83,5 @@ function NavButton({
onPress: () => void;
text: string;
}): JSX.Element {
return (
<Button title={text} onPress={onPress} />
);
return <Button title={text} onPress={onPress} />;
}