- A value that changes depending on a user's actions is called state in React.
- State can be updated.
- Prevent re-rendering
- Three steps to use state:
- Define: State is created using an object in the constructor.
constructor(props) { super(props); this.state = {name: 'Ken the Ninja'}; }
- Display: Since
this.state
is an object, you can get the value of any property by writing code likethis.state.propertyName
- Update: With the code
this.setState({propertyName: valueToUpdate})
, the value of state for the specified property changes.setState()
method is used.
- Define: State is created using an object in the constructor.
- In React, you can pass props, or properties, to child components.