redux-query-sync icon indicating copy to clipboard operation
redux-query-sync copied to clipboard

Read defaultValue from default store state.

Open Treora opened this issue 8 years ago • 0 comments

Rather than setting the defaultValue for each parameter separately, and to have a single point of truth, I would like the default value to be inferred automatically from the store's default state (by applying the selectors to it).

Question is: what is the default state? In theory, it should be what reducer(undefined) returns. Some people might however want to consider initialState as their default, or the state after createStore has run its reducer(initialState, INIT) (here), whereas this would break in for people that let their server side provide an initialState object based on the URL query parameters (we would then use the parameters, but hide them from the URL again). One option is to leave it up to the user to provide the default state:

const store = createStore(reducer, initialState)
const stopSync = ReduxQuerySync({
  store,
  params: {...},
  // For cases where initialState already incorporated the URL's query parameter values.
  defaultState: reducer(undefined, {type: 'dummyInit'}),
  // For cases where initialState is used as the default state.
  defaultState: initialState,
  // Or cases where both initialState and the reducer's initial response to it.
  defaultState: store.getState(),
})

The ReduxQuerySync.enhancer approach would have access to reducer and store, and could do any such a step for us, though it may need an option to choose which approach to take.

Treora avatar Jul 12 '17 10:07 Treora