React JS States

States in Class  

  • State in React is an object that holds values that can change over time. It is same as member variable in class.
  • The state can be updated using the setState() method, which triggers a re-render of the component and its children.
  • The  state should only be modified using setState() and should not be directly mutated.
  •  Accessing and rendering state values in a component’s UI is done using curly braces ({}) within JSX.

We have created constructor in class to have initial state. in the below example there are 2 state: age and address.

the áge’state has been accessed like {this.state.age}

index.js

Output:

Hello, World!

This is a example of class component in JSX.

My name is Shrikant from Person class component and age is 30

_____________________________________________________________

2) States in functional components is implemented using the useState hook.

 which we will discuss in  hook  tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top