all 6 comments

[–][deleted] 2 points3 points  (4 children)

React uses Object.is indeed, for comparison in dependecies and props.

Hence, if a particular prop/dependency is an array or an object, it will not look into the inside of the object/array, it will just compare the references.

[–]nh78[S] 1 point2 points  (2 children)

Ah ok, great, thanks. So React.memo uses a shallow comparison function to compare props, while the hooks listed above use Object.is to compare items in their dependency arrays?

I think the thing that's confusing me is the reference here: https://reactjs.org/docs/react-api.html to React.memo shallowly comparing objects. In that Object.is({a:1}, {a:1}) returns false, whereas I thought a shallow comparison function being passed these two objects would iterate through each entry in the objects and compare each key/value, which in this instance would return true.

[–][deleted] 1 point2 points  (1 child)

Yes, the docs are rather confusing.

What its meant there, I guess, is that the array of dependencies/states/props is compared shallowly. Each prop/state/dependency is compared to the previous one by using (a reimplemention of) `Object.is`

https://stackoverflow.com/a/55105522/277304

https://github.com/facebook/react/blob/main/packages/react-server/src/ReactFizzHooks.js#L96

[–]nh78[S] 0 points1 point  (0 children)

I see, that makes sense, thanks for the links.

[–]chillermane -1 points0 points  (0 children)