Why should we not update the state directly?
If you try to update state directly then it won't re-render the component.
//Wrongthis.state.message = 'Hello world';
Instead use setState()
method. It schedules an update to a component's state object. When state changes, the component responds by re-rendering.
//Correctthis.setState({ message: 'Hello World' });
Note: You can directly assign to the state object either in constructor or using latest javascript's class field declaration syntax.
October 23, 2022
3360
Read more
What is React?
November 06, 2022
ReactJSHow to programmatically trigger click event in React?
November 06, 2022
ReactJS