score:30
From the discussion here it seems that the state of Redux reducers should be persisted in a database.
To persist the state or not, it's likely not a concern of Redux at all. It's more up to application logic.
If something happens in an application, like data upload to server, obviously you need to save state (or a slice of the state to a server).
Since network calls are asynchronous, but Redux is synchronous - you need to introduce additional middleware, as redux-thunk or redux-promise.
As sign-up example, you likely need that actions,
export function creatingAccount() {
return { type: 'CREATING_ACCOUNT' };
}
export function accountCreated(account) {
return { type: 'ACCOUNT_CREATED', payload: account };
}
export function accountCreatingFailed(error) {
return { type: 'ACCOUNT_CREATING_FAILED', payload: error };
}
export function createAccount(data, redirectParam) {
return (dispatch) => {
dispatch(creatingAccount());
const url = config.apiUrl + '/auth/signup';
fetch(url).post({ body: data })
.then(account => {
dispatch(accountCreated(account));
})
.catch(err => {
dispatch(accountCreatingFailed(err));
});
};
}
Some portion of state, e.g. user object after authorization, might be stored to localStore
and re-hydrated on next application run.
score:9
Those are valid concerns. Using localStorage
to persist state on the frontend might be a better strategy. You can implement this using middleware, for example:
import {createStore, compose, applyMiddleware} from 'redux';
const localStorageMiddleware = ({getState}) => {
return (next) => (action) => {
const result = next(action);
localStorage.setItem('applicationState', JSON.stringify(
getState()
));
return result;
};
};
const store = compose(
applyMiddleware(
localStorageMiddleware
)
)(createStore)(
reducer,
JSON.parse(localStorage.getItem('applicationState'))
)
If you're concerned about the enemy accessing the user's laptop and stealing credentials from it you could persist state to the backend when the user leaves the page (Navigator.sendBeacon()
might be helpful here) & store it in the session.
Source: stackoverflow.com
Related Query
- Redux state persistence with a database
- Redux Store does not update state with created data on firebase real-time database
- Redux Form - initialValues not updating with state
- Dealing with local state in react and redux
- How do I use local state along with redux store state in the same react component?
- Router state not being persisted in react-native with redux
- Storing objects with prototype functions in Redux state
- Access props sent to components along with Redux state data
- How to organize state with redux and react-router?
- How to compose redux reducers with dependent state
- How can I access state of another slice in redux with redux-toolkit?
- How do I call setState with the previous state plus an additional value in a redux connected controlled component?
- Using redux with a local database
- Can I access state inside a createAsyncThunk w/axios with redux toolkit?
- Update redux state with an input
- Combining React local state with Redux global state
- Should I be concerned with the rate of state change in my React Redux app?
- Sync queryParameters with Redux state and react router for function components
- Redux state doesn't update with action dispatch
- A component is changing the default value state of an uncontrolled Slider after being initialized. Material UI Slider with Redux State
- How to use useEffect hook properly with array dependency. I passed state from redux store and still my component renders infinitely
- I have built a global state redux like pattern with context and hooks. Is there a way to combine reducers?
- How to test redux state update with react testing library and jest
- How to keep the React state in sync with MySQL database
- Replacing Redux with Apollo for local state management
- Get state for child components connected to redux with enzyme
- React Redux store state update process after saving to database
- Content doesn't change when back/forward made on browser with state data in React + Redux
- Redux and libraries with state (cytoscape)
- How to Jump to Redux State with Browser Back Button
More Query from same tag
- TypeError: Assignment to constant variable When combine reducers
- Doing custom hover animation on nav items(bootstrap nav) but the effect isn't visible in my react application
- Functional Component and object values
- Should I always deep clone React's state?
- antd design select placeholder issues
- page is not rendering when I click back button with react-router
- getting null and actual result from fetching an API in react
- Add inside jsx component a variable
- React.js: Unexpected template string expression no-template-curly-in-string
- Incorrect positioning of getBoundingClientRect after newline character
- Module not found: Error: Can't resolve 'crypto', webpack < 5 used to include polyfills for node.js core modules by default
- Equivalent of document.querySelectorAll() in React, onScroll
- How do I manually set input values in ReactJS?
- Organizing and setting state from another file
- React sort array by obj date
- All child elements inheriting onclick event from parent div on React
- Destructured array aliasing for nested objects
- Why cant I separate <Switch> to a different module?
- React Native - NavigationActions.navigate() not navigating from within redux
- I am getting the error "Effect callbacks are synchronous to prevent race conditions. Put the async function inside"
- How to have multiple Providers or state available only for a part of the layout
- show image as selected on click
- What is the type of dispatch clear action?
- What does the at symbol (@) do in ES6 javascript? (ECMAScript 2015)
- Comparing mapped items for additional output using React
- ReactJS: How do I keep my checkbox and radio button checked/selected on page reload?
- How to call Auth0 loginWithRedirect function in React useEffect without button clicking?
- Mock a button click in React with Jest and Enzyme
- Error passing data to display table information using Material-ui
- How to fire change in component from other component?