re: What are Your Best Practices for Writing React Code VIEW POST


When working with a component's state, clone the state while assigning it to a new variable.


let item = this.state.item; // (No)
let item = {...this.state.item}; // (Do this instead)
let item = [...this.state.items] // (Or do this if it's an array)

Ran into issues, especially in multi-person teams, where we'd forget that item references the component's state. item would later get reassigned, or properties get changed, and then component's state is mutated!

code of conduct - report abuse