Why Not To Modify React State Directly

added by JavaScript Kicks
6/1/2018 6:30:24 PM

565 Views

Everybody says don't do it . Never mutate state directly, always call setState. But why, though? If you've tried it out, you might've noticed nothing bad happened. If you modify state directy, call this.setState({}) or even this.forceUpdate(), then everything might appear to be just fine. this.state.cart.push(item.id); this.setState({ cart: this.state.cart }); // renders like normal!


0 comments