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
usePureState #60
Comments
Do you mean it would not re-render if props didn't change? |
Could |
|
I mean that if use useState with object like this.setState, it would be great to skip update using shallow compare of object values like that function Component() {
const [state, setState] = usePureState({ a: 1, b: 2 })
// skip update
setState({ a: 1 }) // merges automatically
// updates
setState({ a: 2})
} |
Yeah, sounds like something useful. I guess, could be easy to do, too, just with just a single loop that shallow-checks all values. |
This is how React do shallowEquals: I would hope that usePureState could get included as a built-in hook. Saves us all re-implementing (read copy-pasting) the above comparator. |
Could this be implemented via a hook that allows a custom comparator function to be used for state updates? It looks like // using default useState comparator
const [ state, setState ] = useDistinctState('a')
setState('A') // will update
...
// using a custom comparator
const [ state, setState ] = useDistinctState('a',
(prev, next) => prev.toUpperCase() === next.toUpperCase()
)
setState('A') // won't update
...
// solving issue use case
function usePureState (initialValue) {
return useDistinctState(initialValue, shallowEqual)
} (I just came from reading #92 so that's part of my consideration here.) |
Use the same as useState but with shallow compare
The text was updated successfully, but these errors were encountered: