Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected PureComponent Behavior? #13881

Closed
GarethSmall opened this issue Oct 18, 2018 · 1 comment
Closed

Unexpected PureComponent Behavior? #13881

GarethSmall opened this issue Oct 18, 2018 · 1 comment

Comments

@GarethSmall
Copy link
Contributor

GarethSmall commented Oct 18, 2018

Do you want to request a feature or report a bug?

bug

What is the current behavior?

Setup:

  • A Component with a counter in it to track the number of renders.
  • A container Component with a counter in it
  • A container PureComponent with with a counter in it.

All three of the above components accept children.

  • A main component that has a button in it that updates the state of some string value once

Scenario 1:

  • I use the regular container component with a single child: the string value state
  • I click the button multiple times
  • It updates each time as expected

Scenario 2:

  • I use the PureComponentContainer with a single child: a static string
  • I click the button multiple times
  • it has only renders 1 times (it's initial render) as expected

Scenario 3:

  • I use the PureComponentContainer with a single child: the string value state
  • I click the button multiple times
  • it has only renders 2 times (it's initial render, and updated render) as expected

Scenario 4:

  • I use the PureComponentContainer with a two children: the string value state, and a static value
  • I click the button multiple times
  • it updates each time I click, although the state value has changed only once. This is not expected

Here is an example where I implement each scenario as well as show a comparison using === and reacts shallowEquals

https://codesandbox.io/s/w0vvkk92nl

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

What is the expected behavior?

I may not fully understand the expected behavior of a PureComponent. I would assume due to the nature of how shallowEqual works it would validate as expected, whether or not the props has the same reference. In my example I show what happens when I compare two example props that both have the same values, but difference references. When I use normal === it doesn't validate if the ref is different, in shallowEqual is validates as long as the values are the same even if the refs are different.

In my second example, the values are the same, and the reference to props doesn't change as expected
In my third example, the values are again the same and the reference to the props again doesn't change as expected

In my final example, the values are the same, but the reference changes.

From this I am lead to believe it's not validating even before it gets to the shallowEqual check.

Maybe my implementation is wrong?

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

Google Chrome Version 70.0.3538.67 (Official Build) beta (64-bit)

16.5.2, I also tested on 16.6.0 alpha.

Mac OS 10.12.6

I haven't verified on previous versions

@GarethSmall
Copy link
Contributor Author

Closing, this seems like a duplicate of: #8669

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant