score:3
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
.
Source: stackoverflow.com
Related Query
- Redux reducer - on state change
- change value of property in state reducer redux
- Change inside key of a state in redux reducer
- Not able to change second value of an array in state with conditional statement in reducer --- Redux
- react redux split reducer change prev state
- How do I change the state to a different object within an array of objects in a Redux Reducer
- Read Store's Initial State in Redux Reducer
- Redux Toolkit: state showing as Proxy / undefined within reducer
- Update Redux state on route change
- How to change the Redux state based on an Electron menu click?
- Props not updating when redux state change in React Hooks
- Format date in Redux Reducer before it hits state
- How to respond to state change in redux and dispatch another action?
- how and when to call a react component methods after state change from redux
- redux - reducer state is blank
- Access other parts of state in Redux reducer
- Should I be concerned with the rate of state change in my React Redux app?
- What is the best way to implement undo state change (undo store/history implementation) in React Redux
- Replacing new state in react redux reducer without making a copy
- How change value of nested array in redux reducer
- Reactjs Redux should we create sub reducer for every object in the state tree?
- Redux Simple Router - Change state AND URL
- How to wait for a Redux action to change state from a React component
- Content doesn't change when back/forward made on browser with state data in React + Redux
- Change value of a key in array of objects in redux reducer
- Updating state on route change with React Redux
- Common loading state reducer with Redux toolkit
- Delete element from array in redux state using a reducer in createSlice
- redux dispatch action and reducer with new state
- Destructing default state in redux reducer
More Query from same tag
- Unable to setState inside child component using the value passed by Parent
- Defining Mongoose Schema with createdAt variable using new Date() doesnt work
- Understanding Redux's Reducer storing State
- Sorting a Props array of objects by date before passing to child
- Show validation when user selects specific radio button
- Call a method into the class React ES6
- D3 + ReactJS network graph hangs the browser indefinitely after continuously updating graph
- How to debug JS of a dynamically loaded page?
- ReactJS - How to redirect another page using history.push by passing data?
- Convert file object to array of objects
- How to pass props to component with NavLink
- react search filter with select option
- Splitting an array of info to send request and receive info properly React JavaScript
- React's `memo` drops generics in the returned function
- React-Checking and Unchecking of CheckBox
- covert list into another new list with new property name
- react.js text div wont scale with my page but the image div will
- Content-Security-Policy will not load images from external sources in React
- How to spy on es6 class methods that are set at run time using Jest
- Is useRef hook a good use case for changing the style of individual item than useState?
- React.js and PHP together?
- ReactJS Can't get value from form input
- Semantic UI React: fixed sidebar & header
- React JS state doesn't update on click
- React Big Calendar shows only certain range of time
- Unable to export multiple cloud functions
- How to change the Redux state based on an Electron menu click?
- Router push not working from within useEffect hook
- Design pattern for working with Ajax data. What is the right way?
- app.get('/') data doesn't show in same url using app.use('/') in express