Idea is to provide withState with _resetable_ state.
Sometimes when I work with react-router (not only) I don't want to recreate component to reset state after route has changed.
The idea is to provide withStateSelector(stateName, stateUpdaterName, selectorFactory) HOC which will reset - recalculate stateValue every time selector return a new result.
This will be easy to use with reselect library. Like
withStateSelector(
'markers', 'setMarkers',
() => createSelector(
({ route: { markersCount = 20 } }) => markersCount,
(markersCount) => generateMarkers(markersCount)
)
),
The real code with example:
Usage
Routes with params
withStateSelector source
Here every time route changed and got new markersCount param createSelector will return a new result, and state will be resetted.
PS: It's useful with controls like forms, which should use internal state until save 'button' / 'blur event' is pressed / occured.
Also we could just extend withState - and to detect selectors if result of initial state call is function.
Also to make it more same with reselect it's possible to pass to selector also the current state,
this.selector(this.state.stateValue, nextProps)
+1 I need it too
Most helpful comment
+1 I need it too