@krebeDev
Posted
I appreciate your feedback @mattstuddert.
As a beginner, working with React was quite tough, but exciting nonetheless.
- I didn't know assigning
e.currentTarget.id
touserChoice.name
will directly mutate theuserChoice
object instate
, because I started off by cloninguserChoice
andhouseChoice
like so:
handlePick = (e) => {
const userChoice = {...this.state.userChoice};
const houseChoice = {...this.state.houseChoice};
// Other code lines go here
}
Do I have to clone/setState for each key : value pair in this instance?
I've started learning about hooks, and definitely will apply them once I get the hang of it.
- On the identical components, I'll refactor my code as you suggested. I just noticed the same repetitive pattern in my
Starter
component too. I'll fix them.
@mattstuddert
Posted
@krebeDev hey Solomon. As you said because you're cloning the state objects you're not directly mutating state. But what I'm saying is that you don't need to do this at all. Instead, you can use the this.setState()
method to update the state of the component. I'd recommend taking a look into the React docs for the setState
method as it will clean up your code. Also, once you do learn about hooks it will change the way you approach your component architecture for sure.
@krebeDev
Posted
Hi @mattstuddert. Thanks again. I get your point now. I'm still on the React journey, so yeah, I'll take a deep look at the docs.
@mattstuddert
Posted
@krebeDev no problem! The docs are great and explain the concepts really well. I look forward to seeing your next React project!