question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A doubt behaviour using the PureComponent

See original GitHub issue

What is the current behavior?

I’ve met a strange behaviour using PureComponent:

It’s normal to unincrease the number when click the add button as follow(shallowEqual return true):

class B extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
    this.click = this.click.bind(this);
  }

  click() {
    this.setState({
      count: ++this.state.count
    });
  }

  render() {
    return (
      <div>
        <button onClick={this.click}>add</button>
        <div>{this.state.count}</div>
      </div>
    );
  }
}

but I’m doubt it’s normal to increase the number when using like follow: link

click() {
  const state = Object.assign({}, this.state);

  this.setState({
    count: ++state.count
  });
}

In my option, the effect of { count: ++this.state.count } is same as { count: ++state.count }. I don’t know why the second way can do it ? Thanks for replying.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
AliasTcommented, Aug 20, 2018

@MuYunyun, your ++this.state.count have already change the state to new version

const t = ++this.state.count
console.log(t === this.state.count) // => true
this.setState({
  count: t
})
2reactions
MuYunyuncommented, Aug 20, 2018

@AliasT It’s surprised. 👍 I understand, Thanks.

I’ve writen a post about this issue in Chinese.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why are all the PureComponent children of a container ...
I modified the Todo from a functional component to PureComponent class so that shouldComponentUpdate() returns false if all the prop references ...
Read more >
React PureComponents and Hooks function components | by July ...
Migrating from PureComponent to functional component, wrapping the component with React.memo provides the same re-rendering behavior as PureComponent. The props ...
Read more >
State Management in Pure React with Hooks: useEffect
The useEffect() Hook takes two arguments. The first argument is a function in which you can perform your side effects, and the second...
Read more >
Nadia Makarevich on LinkedIn: PureComponents vs Functional ...
Probably something on data fetching in React, but I'm open to other ideas as well: if you or your team is struggling with...
Read more >
Optimizing React component event handlers - Nicholas Tsim
The React team are aware that this can be a performance trap, so they encourage us to use memo or PureComponent on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found