0

New react enthusiast here needing a little bit of help.

So I have this set of array store in a state called projects

0:{id: 1, title: "Business Web", category: "Web Design", deleted_at: "0000-00-00 00:00:00"}
1:{id: 2, title: "Social App", category: "Mobile Development", deleted_at: "0000-00-00 00:00:00"}
2:{id: 3, title: "Ecommerce", category: "Web Development", deleted_at: "0000-00-00 00:00:00"}
3:{id: 4, title: "1", category: "1", deleted_at: "0000-00-00 00:00:00"}
4:{id: 5, title: "123123", category: "123123", deleted_at: "0000-00-00 00:00:00"}
5:{id: 6, title: "new", category: "new", deleted_at: "0000-00-00 00:00:00"}
6:{id: 7, title: "sdasd", category: "sdawd", deleted_at: "0000-00-00 00:00:00"}
7:{id: 8, title: "sssss", category: "ssssss", deleted_at: "0000-00-00 00:00:00"}
8:{id: 9, title: "Irene", category: "Bae", deleted_at: "0000-00-00 00:00:00"}
9:{id: 10, title: "sssss", category: "sssss", deleted_at: "0000-00-00 00:00:00"}

And Im using this to this to store an item into state called projectItem

this.setState({
   projectItem: this.state.projects.filter(p => p.id === id)
})

which give me a result of something like this..

0: {id: 1, title: "Business Web", category: "Web Design", deleted_at: "0000-00-00 00:00:00"}

Now when I try to access this state and set it as initial value for input, its not working. I set value like this

value={this.state.projectItem.title}

Can you please help me where did I go wrong? Or am I doing this the wrong way? Thank you in advance.

2
  • Are you actually storing your filtered element into setState, or are you just assuming that's what's happening? What error are you getting? Where is id coming from? This is not a complete example. Commented Mar 20, 2018 at 20:57
  • Actually I dont get any errors. And yes its storing in the projectItem state but I cant access the items. Commented Mar 20, 2018 at 21:01

2 Answers 2

1

Array.prototype.filter returns an array, so to get an item you may do something like

cont filtered = this.state.projects.filter(p => p.id === id);
this.setState({
  projectItem: filtered.length ? filtered[0] : null
});

Maybe you want to find item instead:

this.setState({
  projectItem: this.state.projects.find(p => p.id === id);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Wow. That works like a charm. Can you explain to me how this works? and where did I go wrong? Anyways thanks kind sir.
@SonsonIxon Just use find instead of filter. I updated the answer with the links to MDN, which can be helpful for you.
0

Your issue is that filter returns an array and not a single object.

Try assigning

this.setState(
    projectItem: this.state.projects.filter(p => p.id === id)[0]
})

2 Comments

Wow. This works too. I have search how to flatten an array but none of it works. This is amazing.
You don't need to flatten an array, you need the content of the array item to be assigned to the value on the input tag. In your original code you assign the array itself as the value of the attribute

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.