0

I'm having some difficulty setting variables inside my handleSubmit function. this.props.posts is an array of objects coming from redux. I simply want to grab a single post object that matches with the id of the prop I passed in. I think my confusion lies in not quite understanding what I'm actually supposed to be returning here. Any clarification would be appreciated. Thanks!

handleSubmit = (e) => {
e.preventDefault()
const  post  = 
  this.props.posts.map((el, i) => {
    if (el.id === this.props.id) {
      return el
    }
  })
console.log(post)
this.props.dispatch(updatePost(this.state.post))
this.props.closeForm()
}

1 Answer 1

1

Array.prototype.map isn't what you're looking for. You should use the Array.prototype.find() method to locate the object by id.

const  post  = this.props.posts.find(el => el.id === this.props.id)

or something like that.

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

1 Comment

Worked perfectly. Thanks!

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.