score:1
Accepted answer
apparently you are trying to save the state to the localstorage
and load it after reloading the page
i think that you do not need to create a promise
at the moment when you load the state from the localestorage
try to initialize the store
as shown below
store.js
import {createstore} from "redux";
import appreducer from "./appreducer";
export const loadstate = () => {
try {
const state = localstorage.getitem('state');
if (state === null) {
return undefined;
}
return json.parse(state);
} catch (err) {
return undefined;
}
};
export const savestate = (state) => {
try {
const st = json.stringify(state);
localstorage.setitem('state', st);
} catch (err) {
}
};
const throttle = (func, limit) => {
let lastfunc;
let lastran;
return function() {
const context = this;
const args = arguments;
if (!lastran) {
func.apply(context, args);
lastran = date.now();
} else {
cleartimeout(lastfunc);
lastfunc = settimeout(function() {
if ((date.now() - lastran) >= limit) {
func.apply(context, args);
lastran = date.now()
}
}, limit - (date.now() - lastran))
}
}
};
const store = createstore(appreducer, loadstate());
store.subscribe(throttle(() => {
// this callback is called on every state change.
// here your current state is written to the localstorage
savestate(store.getstate());
}, 2000));
export default store;
Source: stackoverflow.com
Related Query
- Using Redux default store from window in React app
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- How to get the data from Redux store using class components in React
- How do I stop state store data from accumulating in a redux component every time I navigate to it using react router
- React presentational component unable to read value for <input /> from redux store using react container
- cannot import store from redux library in react app
- React apollo default query variable from redux store
- Unable to read data from Redux store in the React app
- How can I update react app that gets it's data from the node mongo backend using redux
- Accessing Redux Store from routes set up via React Router
- Prevent react component from rendering twice when using redux with componentWillMount
- React get state from Redux store within useEffect
- Detect network connection in React Redux app - if offline, hide component from user
- How to reset state of Redux Store when using configureStore from @reduxjs/toolkit?
- What is the necessity of "export default connect" when you are connecting your react component to redux store
- Where to store WebRTC streams when building React app with redux
- Access Redux store outside React when using next-redux-wrapper
- React Native: HeadslessJS and Redux - How to access store from task
- Developing a Redux store as a seperate NPM module for a React App
- Best way to store file objects in React Redux store (files upload from DropZone?)
- How to execute store.unsubscribe from useEffect using React hooks and Redux
- My React app is using values from the .env file instead of the .env.local file
- Invoke Lambda function from Amplify-generated React App without using API Gateway
- Using OAuth2 refresh token in React Redux app
- Why is this variable from the Redux store not in the `this.props` on React page?
- Redux save store as global variable on React app
- Structuring the store of a localized react / redux app
- React Redux Toolkit: How can I can store my initialState to localStorage using createSlice?
- How to retrieve data from Redux store using functional components and avoiding useSelector & useDispatch
- How to update React UI from with state in Redux store with useEffect
More Query from same tag
- importing .scss with ReactJs
- How to manipulate inside of a component in React?
- React component not updating after redux store is populated
- How can I fix an issue with create-react-app where only node_modules, package.json, and package-lock.json are created?
- How do I apply CSS transitions (e.g. fade-ins) to a list of items in React so their animations are sequenced one after the other on render?
- react save possibility to select text with re-render component
- "Chaining" Meteor subscriptions
- Why NextJS using Docker container did not reload after changed code for dev environment?
- Best practice when asserting non-null on a value coming from React.useContext (or anywhere else really)
- Navigating from React to a non React web page on the same domain
- react, cannot pass props to next level
- How do I add variable to the POST body of the request
- Unable to read props data object in getInitialState and ComponentDidMount in the child component in reactjs
- React: Is there a way to capture changes to props sent via history.push
- How to display data by passing it through props?
- Multiple events in the same time with react big calendar
- ReactGA.initialize must be called first
- The input value in a React controlled form is insensible to keystrokes
- How to detect screen orientation in mobile website when using react js and orientationchange event?
- React child component state updates parent component state
- Can using .map() twice over the same array be avoided even though the components the map() renders are not siblings?
- Several instances of 'styled-components' initialized in CRA micro-frontend application
- Auto resize font-size in react material-ui Button for unexpected long content?
- How to pass a prop back from child to parent in onclick handler and then to another component in React?
- Ternary operator multiple actions in reactjs
- TypeError: Cannot read property 'pathname' of null
- Module build failed: SyntaxError: Unexpected token (11:16)
- Looking to conditionally call a mutation
- Dynamic route not working on page refresh with Next.js
- Using babel-polyfill with webpack