最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the difference between React component instance property and state property? - Stack Overflow

programmeradmin6浏览0评论

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 ponent state?

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 ponent state?

Share Improve this question asked Nov 19, 2018 at 12:45 lsklsk 1431 silver badge5 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 11

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 paring prevState and currentState in shouldComponentUpdate to decide whether you want to render or not, or in lifecycle method like ponentDidUpdate 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 ponent, a change in this class variable can't be differentiated in the lifecycle methods of the child ponent unless you are creating a new instance of the variable yourself. React does it with state property for you already.

Both have different purpose. Rule of thumb is:

  • Use state to store data if it is involved in rendering or data flow (i.e. if its used directly or indirectly in render method)
  • Use other instance fields to store data if value is NOT involved in rendering or data flow (to prevent rendering on change of data) e.g. to store a timer ID that is not used in render method. See TimerID example in official docs to understand this valid case.

If some value isn’t used for rendering or data flow (for example, a timer ID), you don’t have to put it in the state. Such values can be defined as fields on the ponent instance.

Reference: https://reactjs/docs/react-ponent.html#state

发布评论

评论列表(0)

  1. 暂无评论