-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Shallow equality function as 2nd argument for useStoreState() (#…
…354) Currently. useStoreState() performs a strict equality check (`oldState === newState`) on its state, which is great for most cases. But sometimes you need to work with more complex state and return something that will always fail a strict check. This PR adds support for providing a custom equality function as follows: ```javascript const store = createStore({ count: 1, firstName: null, lastName: null }); // In your component const { count, firstName } = useStoreState( state => ({ count: state.count, firstName: state.firstName, }), (prevState, nextState) => { // perform some equality comparison here return shallowEqual(prevState, nextState) } ); ``` In the above case, if either the `count` or `firstName` state vars change, the equality check with be true, and the component will re-render. And if any other state is changed; for example, the `lastName`, the equality check will be true, and the component will not re-render. An exported `shallowEqual()` function is provided to allow you to run shallow equality checks: `useStoreState(map, shallowEqual)`. See #275 Co-authored-by: Sean Matheson <[email protected]>
- Loading branch information
Showing
7 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters