You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 10, 2021. It is now read-only.
Just for curiosity, why you've opted for a multi-store subscription model instead of subscribing to the store in a top-level Provider?
Something like:
constReduxStateContext=React.createContext()functionReduxStateProvider({ store, children }){const[state,setState]=React.useState(()=>store.getState())useEffect(()=>{letwillUnsubscribe=falseconstcheckForUpdates=()=>{if(willUnsubscribe)returnsetState(prevState=>{constnextState=store.getState()returnshallowEqual(prevState,nextState) ? prevState : nextState})}checkForUpdates()constunsubscribe=store.subscribe(checkForUpdates)return()=>{willUnsubscribe=trueunsubscribe()}},[store])return(<ReduxStateContext.Providervalue={state}>{children}</ReduxStateContext.Provider>)}exportfunctionuseMappedState(mapState){conststate=useContext(ReduxStateContext)const[derivedState,setDerivedState]=useState(()=>mapState(state))useEffect(()=>{setDerivedState(prevDerivedState=>{constnextDerivedState=mapState(state)returnshallowEqual(prevDerivedState,nextDerivedState)
? prevDerivedState
: nextDerivedState})},[state])// It might not even need useEffect() 🤔 (getDerivedStateFromProps)setDerivedState(prevDerivedState=>{constnextDerivedState=mapState(state)returnshallowEqual(prevDerivedState,nextDerivedState)
? prevDerivedState
: nextDerivedState})returnderivedState}
Only the ReduxStateProvider subscribes to store updates, then it passes the update down to all consumers. Consumers have a chance to bail out by comparing prevDerivedState with nextDerivedState.
The text was updated successfully, but these errors were encountered:
Every single action that is dispatched via redux and change any place of the state would trigger a rerender on all components that are using useMappedState simply because it's using the line above.
Context updates triggers a rerender just like useState updates and you can't bail out of context updates yet, see item 2: facebook/react#14110
But then the consumer will bail out via setDerivedState. This same problem happens to the multi store approach, because every hook subscribes to the store.
Just for curiosity, why you've opted for a multi-store subscription model instead of subscribing to the store in a top-level Provider?
Something like:
Only the
ReduxStateProvider
subscribes to store updates, then it passes the update down to all consumers. Consumers have a chance to bail out by comparingprevDerivedState
withnextDerivedState
.The text was updated successfully, but these errors were encountered: