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
I've read The History and Implementation of React-Redux few days ago and I have several statements and questions:
I do not want use context for performance reason (as in v6), let's not affect React flow after each action if it doesn't change anything.
For the first statement I have question, is there any problem to subscribe on store inside single effect and call updates only when we have changes? Is it problem of making calls continuously depending on components tree?
Do we actually need Provider?
Looks like dispatching actions by action creator should work as useCallback and don't change anything at all.
The implementation of state mapper should give us an ability to use our selectors without additional effort.
Passing store reference to actual components:
<ReduxStoreProviderstore={store}>
// ...
</ReduxStoreProvider>constReduxUpdateLessContext=React.createContext(null);functionReduxStoreProvider({ store }){// Store object isn't changing? So let's pass only reference to it.// Don't affect react flow each actionconststoreReference=useRef(store);return(<ReduxUpdateLessContext.Providervalue={storeReference}>{children}</ReduxUpdateLessContext.Provider>}// Then use store in effectsconststoreReference=useContext(ReduxUpdateLessContext);storeReference.current.getState();storeReference.current.dispatch(action);
useSelectState idea:
// Looks like we should create mapper function and memoize it,// and pass new properties if they are changed as it was in connect function.// Return the same object reference if nothing is changedconst{ key1, key2 }=useSelectState((state,[dep1,dep2])=>({key1: selectorA(state,dep1),key2: selectorB(state,dep2),}),[propsDep1,propsDep2])functionuseSelectState(mapper,propsArray){conststoreReference=useContext(ReduxUpdateLessContext);constmapperCallback=useCallback((state)=>mapper(propsArray),propsArray);const[mappedState,setMappedState]=useState(mapperCallback(storeReference.current.getState()));useEffect(()=>{conststore=storeReference.current;letpreviousMappedState=mappedState;functiononStoreChanged(){constnextMappedState=mapperCallback(store.getState());if(shallowEq(previousMappedState,nextMappedState)){setMappedState(nextMappedState);}}// get store somehow without context?constcleanupStoreSubscribe=store.subscribe(onStoreChanged);returncleanupStoreSubscribe;},[],// prevent calling twice);}
useActionCreator(s) idea:
// Do we need to know about name `dispatch` function if we have only action creators// It was good to know when we had special mapper outside `react-redux`const{ requestA }=useActionCreators({requestA: requestACreator,})// Do we need several `useActionCreator` hooks?// Is it excessively?constmethodA=useActionCreator(actionCreatorA);constmethodB=useActionCreator(actionCreatorB);constmethodC=useActionCreator(actionCreatorC);// Something like?functionuseActionCreator(actionCreator){conststoreReference=useContext(ReduxUpdateLessContext);// get dispatch method from Store without making additional Consumer// somehow!?returnuseCallback((...args)=>{storeReference.current.dispatch(actionCreator(...args));})}
The text was updated successfully, but these errors were encountered:
I've read
The History and Implementation of React-Redux
few days ago and I have several statements and questions:useCallback
and don't change anything at all.Passing store reference to actual components:
useSelectState
idea:useActionCreator(s)
idea:The text was updated successfully, but these errors were encountered: