I'm trying to build an infinite scroll of posts similar to facebook using react-infinite-scroller. However, it's giving me the same error multiple times in the console -
"Encountered two children with the same key,
shdj1289
. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version."
This is the Render function
render() {
const { loading, userData, circleData, redirectToLogin, posts } = this.state;
return redirectToLogin ? <Redirect to="/" /> : (
<Container>
<h1>Hi {userData.firstName}</h1>
<InfiniteScroll
loadMore={this.fetchPosts}
hasMore={this.state.hasMore}
useWindow={false}
loader={<div key={0}></div>}
>
{posts.map(p => {
return (<Post key={p.postIdentity} data={p} />);
})}
</InfiniteScroll>
</Container>
)
}
This is the fetchPosts function -
fetchPosts = () => {
const postQuery = {
"PageIndex": this.state.start + 1,
"PageSize": this.state.count,
"Id": "shjhdjs"
}
request({
url: 'http://www.example.com/posts',
method: 'POST',
data: postQuery
}).then((res) => {
if(res.data.length === 0) return this.setState({ hasMore: false });
this.setState({ start: this.state.start + 1, posts: this.state.posts.concat(res.data)});
}).catch((err) => console.log(err))
}
This is the initial state -
state = {
start: 0,
count: 10,
hasMore: true
}
Any ideas on what I might be doing wrong? Whenever I run this code, the posts in page 1 get rendered twice and I see these errors in the console -
As you can see the request with PageIndex = 1 is being called twice which is the reason the warnings are popping up.
Not sure what I'm missing. Any suggestions would be highly appreciated. Thank you!