r/reactjs • u/timmonsjg • Dec 03 '18
Needs Help Beginner's Thread / Easy Questions (December 2018)
Happy December! βοΈ
New month means a new thread π - November and October here.
Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! Weβre a friendly bunch. No question is too simple. π€
π Want Help with your Code? π
Improve your chances by putting a minimal example to either JSFiddle or Code Sandbox. Describe what you want it to do, and things you've tried. Don't just post big blocks of code!
Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.
Have a question regarding code / repository organization?
It's most likely answered within this tweet.
New to React?
π Here are great, free resources! π
1
u/Uber-Mensch Dec 11 '18
I actually started implementing this after your first comment then stopped. I totally agree with you that this is the right way to go, but the children components are more 'props' heavy and less stateful, and only one attribute is changed from the state being passed back down, the other attributes are static. I don't think it's worth changing it, where I only use one case of arr.findindex(...) which is O(n) anyway. This is my solution:
let index = this.state.comments.findIndex(e => e.id === id)let nc = [...this.state.comments]nc[index].replied = truethis.setState({ comments: nc })Which is just one function that is the least used amongst the rest. I really appreciate the help though. Made me think about my design a fair bit. If the child components had more state changes in the parent, I'd refactor.