Props and State

Props and State

Props and state

Props

Props stand for "properties", props is an object which holds the values of attributes of a tag, props are immutable, which means we can not change the value of props, props give way to pass data from one component to another component.

How to access props?

Suppose we have passed the name as a prop.

➖In functional "props.name",

➖In class component "this.props.name"

State

The state is an updatable structure that is used to contain data or information about the component, The state in a component state can changed over time, because of user actions.

◽ Defining state ➖We define a state inside a constructor

this.state ={ name : "Adarsh" }

◽ for access {this.state.name}

◽ for update this.setState({name : "Rahul"})

In a functional component, we use a hook for managing the state.

Difference between Props and State

Props

➖ Props get passed to the component.

➖ Props are immutable.

➖ Props In functional component => e.g props.name;

➖ Props In class component => e.g this.props.name;

State

〰 State is managed within the component.

〰 State can be changed.

〰 State In functional component => with the help of useState hook

〰 State in class component => this.state