What is state in React?

Practice



State of a component is an object that holds some information that may change during the component's lifetime. We should always try to make our state as simple as possible and minimize the number of stateful components. Let's create a custom component with a message state,

class User extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      message: 'Welcome to React world'
    }
  }

  render() {
    return (
      

{this.state.message}

) } }
State is similar to props, but it is private and fully controlled by the component. That is, it is not accessible to any component other than the one that owns and sets it.

What is state in React?

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Famworks"

Terms: Famworks