Recompose: withStateSelector proposal

Created on 13 Jun 2016  路  3Comments  路  Source: acdlite/recompose

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.

Most helpful comment

+1 I need it too

All 3 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cdomigan picture cdomigan  路  4Comments

jethrolarson picture jethrolarson  路  4Comments

robbporto picture robbporto  路  3Comments

uriklar picture uriklar  路  4Comments

nemocurcic picture nemocurcic  路  3Comments