-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Reuse latest selected state on selector re-run (#1654) #1660
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -412,6 +412,35 @@ describe('React', () => { | |
|
||
spy.mockRestore() | ||
}) | ||
|
||
it('reuse latest selected state on selector re-run', () => { | ||
store = createStore(({ count } = { count: -1 }) => ({ | ||
count: count + 1, | ||
})) | ||
|
||
const alwaysEqual = () => true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this also maintain the latest state if the equalityFn is defined within the component or inline? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes—it only cares what the equalityFn returns, not what its identity is. However, having a non-pure equalityFn (i.e., one that closes over variables from its surrounding components) could have very surprising results. |
||
|
||
const Comp = () => { | ||
// triggers render on store change | ||
useSelector((s) => s.count) | ||
const array = useSelector(() => [1, 2, 3], alwaysEqual) | ||
renderedItems.push(array) | ||
return <div /> | ||
} | ||
|
||
rtl.render( | ||
<ProviderMock store={store}> | ||
<Comp /> | ||
</ProviderMock> | ||
) | ||
|
||
expect(renderedItems.length).toBe(1) | ||
|
||
store.dispatch({ type: '' }) | ||
|
||
expect(renderedItems.length).toBe(2) | ||
expect(renderedItems[0]).toBe(renderedItems[1]) | ||
}) | ||
}) | ||
|
||
describe('error handling for invalid arguments', () => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.