Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between React component instance property and state property?

Consider the below example

class MyApp extends Component {
    counter = 0;
    state = {
        counter: 0
    };
    incrementCounter() {
        this.counter = this.counter + 1;
        this.setState({
            counter: this.state.counter + 1
        });
    }
    render() {
        return <div>
            <p>{this.counter} and {this.state.counter}</p>
            <button onClick={this.incrementCounter}>Increment</button>
        </div>
    }
}

When I click the button I see both this.counter and this.state.counter are showing the incremented value

My question is why I have to use state? though react is capable of re-rendering all the instance properties

counter = 0;
incrementCounter() {
    this.counter = this.counter + 1;
    this.setState({});
}

In above snippet, just calling this.setState({}) is doing the trick, then why should I use this.state property for storing my component state?

like image 573
lsk Avatar asked Nov 19 '18 12:11

lsk


People also ask

What is an instance of a React component?

An instance is what you refer to as this in the component class you write. It is useful for storing local state and reacting to the lifecycle events. Function components don't have instances at all. Class components have instances, but you never need to create a component instance directly—React takes care of this.

What is state property in React?

What Is 'State' in ReactJS? The state is a built-in React object that is used to contain data or information about the component. A component's state can change over time; whenever it changes, the component re-renders.

What are component properties in React?

Components are independent and reusable bits of code. They serve the same purpose as JavaScript functions, but work in isolation and return HTML. Components come in two types, Class components and Function components, in this tutorial we will concentrate on Function components.

What is the difference between props and state in React component?

Props are used to pass data from one component to another. The state is a local data storage that is local to the component only and cannot be passed to other components. The this. setState property is used to update the state values in the component.


1 Answers

state and instance properties serve different purposes. While calling setState with empty arguments will cause a render and will reflect the updated instance properties, state can be used for many more features like comparing prevState and currentState in shouldComponentUpdate to decide whether you want to render or not, or in lifecycle method like componentDidUpdate where you can take an action based on state change.

state is a special instance property used by react to serve special purposes. Also in setState, state updates are batched for performance reasons and state updates happen asynchronously unlike class variable updates which happen synchronously. A class variable won't have these features.

Also when you supply a class variable as prop to the component, a change in this class variable can't be differentiated in the lifecycle methods of the child component unless you are creating a new instance of the variable yourself. React does it with state property for you already.

like image 155
Shubham Khatri Avatar answered Oct 25 '22 05:10

Shubham Khatri