DEV Community

Discussion on: What are Your Best Practices for Writing React Code

ganderzz profile image
Dylan Paulus • Edited on

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!