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 has been archived by the owner on Sep 10, 2022. It is now read-only.
I found myself trying to use withState to recreate my own HOC that has the same state & setState API that a normal React class component has. The behavior (as we all probably know) is where setState() merges passed objects with existing state. The API is perfectly suited for managing local state.
I then wondered would it be more generally useful and also immediately familiar if withState mimicked the React state api?
Example:
constinitialState={foo: true};constenhance=withState(initialState);// only 1 param neededconstExample=enhance(({ state, setState })=>// always 'state' and 'setState'<div><buttononClick={()=>setState({bar: true})}/><div>{state.foo} and {state.bar}</div></div>);
Notice how I arbitrarily added bar. This version is very flexible.
I do like the current withState, but Recompose is the building blocks for custom HOCs. The current withState would be more easily composed using the withState above other than vice versa. Like so:
constlegacyWithState=(name,method,initial)=>compose(withState({val: initial}),mapProps(({ state, setState })=>({[name]: state.val,[method]: (val)=>setState({ val })})));
Thoughts?
The text was updated successfully, but these errors were encountered:
I found myself trying to use
withState
to recreate my own HOC that has the samestate
&setState
API that a normal React class component has. The behavior (as we all probably know) is wheresetState()
merges passed objects with existing state. The API is perfectly suited for managing local state.I then wondered would it be more generally useful and also immediately familiar if
withState
mimicked the React state api?Example:
Notice how I arbitrarily added
bar
. This version is very flexible.I do like the current
withState
, but Recompose is the building blocks for custom HOCs. The currentwithState
would be more easily composed using thewithState
above other than vice versa. Like so:Thoughts?
The text was updated successfully, but these errors were encountered: