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
Hi Diego! Thinking of using this in addition to or instead of zustand. One thing I like about zustand is that you can select from the state when you use the hook, like so:
// If you don't pass anything to the hook it returns everything, and rerenders when anything changesconstfoo=useMyStore()// However, if you pass a selector fn, then it will only "watch" the selected state.// Good for perf optimizations.constfoo=useMyStore(state=>state.foo)// By default it's a referential equality check, but you can pass different equality fns.// So you could pass lodash's isEqual to do a deep equal comparison if you needed to.constsomeBigObj=useMyStore(state=>state.someBigObj,_.isEqual)
Do you think it's possible to accomplish this with constate? Like this?
importReact,{useState}from"react";importcreateStorefrom"zustand";// 1️⃣ Create a custom hook as usualfunctionuseCounter(){const[count,setCount]=useState(0);constincrement=()=>setCount(prevCount=>prevCount+1);return{ count, increment };}// 2️⃣ Wrap your hookconst[Provider,useCounterContext]=constate(useCounter)functionButton(){// 3️⃣ Use store instead of custom hook, make use of selector apiconstincrement=useCounterContext(s=>s.increment);return<buttononClick={increment}>+</button>;}functionCount(){// 4️⃣ Use store in other componentsconstcount=useCounterContext(s=>s.count);return<span>{count}</span>;}functionApp(){return(<Provider><Count/><Button/></Provider>);}
The text was updated successfully, but these errors were encountered:
I'm not sure we can do that, especially the No need for a Provider part. Hooks need to run at the component render phase, and with Constate they run on the Provider component.
Because of that, and because we can't bail out from rendering when using React.useContext, there'll be no difference between these two:
constcount=useCounterContext(s=>s.count);// would be the same asconst{ count }=useCounterContext();
Hi Diego! Thinking of using this in addition to or instead of zustand. One thing I like about zustand is that you can select from the state when you use the hook, like so:
Do you think it's possible to accomplish this with constate? Like this?
The text was updated successfully, but these errors were encountered: