score:3

Accepted answer

unfortunately you can't watch specific parts of your store because your store is actually an only reducer (combinereducers return a single big reducer). that said you can manually check if your reducer has changed by doing state part comparison.

let previousmyreducer = store.getstate().myreducer;
store.subscribe(() => {
  const { myreducer } = store.getstate();
  if (previousmyreducer !== myreducer) {
    // store myreducer to sessionstorage here
    previousmyreducer = myreducer;
  }
})

score:0

you can use redux-saga to listen for specific actions and persist the store (and do your other logice) when they are fired. this seems like a good fit for your needs.

it's not exactly tied to specific store changes, but rather to a set of actions. however, i think this model (listening for actions, as opposed to state changes) is better suited to the redux design.

the nice thing is you don't have to change any code outside of the saga. i've used this one for autosaving form data and it worked great.

here's an example of setting up a saga to persist on a few redux-form actions; note that it's not limited to persisting - you can do whatever you want during a saga.

function* autosavesaga(apiclient) {
  yield throttle(500,
  reduxformactiontypes.change,
    saveformvalues,
    apiclient);

  yield takelatest(
    reduxformactiontypes.array_push,
    saveformvalues,
    apiclient);

  yield takelatest(
    reduxformactiontypes.array_pop,
    saveformvalues,
    apiclient);
 }

if you want to listen to all action types, or do custom filtering of which actions fire your persistence code, you can pass a pattern, or a matching function to takelatest:

   yield takelatest(
    '*',
    saveformvalues,
    apiclient);

more on what you can pass to take, takelatest etc can be found in the docs for take.


Related Query

More Query from same tag