1

React is mapping duplicate posts

like this

enter image description hereenter image description here

Ideally im just looking to map the likes array.

  {myLikes.map(like =>     

   )} 

within the posts, i don't want duplicates posts. How would i achieve that ?

PostList.js

    render(){
        const {posts, myLikes} = this.props;
        return (    
        <div>
            {posts.map(post => (
                <div>
                {myLikes.map(like =>  
                <Paper key={post.id} style={Styles.myPaper}>        
                        <PostItem  

                            myLikes={like}                 
                            myTitle={this.state.title} 
                            editChange={this.onChange} 
                            editForm={this.formEditing} 
                            isEditing={this.props.isEditingId === post.id} 
                            removePost={this.removePost} 
                            {...post} 
                            // {...like}
                        />         

                </Paper>
                )}
                </div>
            ))}
        </div>
        )
    }
}
const mapStateToProps = (state) => ({
    isEditingId: state.post.isEditingId,
    myLikes: state.post.likes // reducer likes 
})

enter image description here

Posts

enter image description here

Likes

enter image description here

4
  • Remove duplicate values from the array before using map() , if you are using lodash, try uniqBy() to remove duplicate value. Commented Apr 26, 2019 at 17:54
  • is the problem that you have duplicate posts in array or that the react is rendering one post twice ? Commented Apr 26, 2019 at 17:55
  • react is rendering one post twice. Yes correct. in the console.log their 2 posts not 4. Its the map thats making it do it Commented Apr 26, 2019 at 17:56
  • what will be the correct way of doing it, thanks in advance Commented Apr 26, 2019 at 18:00

2 Answers 2

4

This is actually what you told React to do, without realizing it.

I'm assuming that myLikes is an array of numbers, in this case [32, 15].

Your code says (in pseudocode form):

for each post p
    for each like l
        render the post p with like l

That gives you 2 * 2 = 4 copies.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, what will be the right way to do it. without duplicate post items
1

this is due to nested maps in the PostList component, you can merge the mylikes and post arrays first and then use map

How to merge multiple arrays

this is how it will work

const results = _.merge(myLikes, posts);
return (
  <div>
    {results.map(result => (
      <Paper key={result.id} style={Styles.myPaper}>
        <PostItem
          myLikes={result.like}
          myTitle={this.state.title}
          editChange={this.onChange}
          editForm={this.formEditing}
          isEditing={this.props.isEditingId === post.id}
          removePost={this.removePost}
        />
      </Paper>
    ))}
  </div>
);


7 Comments

getting post is not defined.
result.id how do i know if not using the myLikes id, know what i mean ?
you can import _ from lodash
also how do i know if result.id is not using myLikes.id ?
can you send me the actual array i.e posts and myLikes, so i will update my answer again
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.