如何使用“combineReducers”将 Redux 状态转换为 Redux Persist?
我尝试在 Redux-Persist 中转换我的 Redux 状态,但我不知道如何编写代码,因为我使用 mergeReducers。
这就是我的商店的样子:
import { createStore, combineReducers } from 'redux'
import { usersReducer } from './users';
import { eventsReducer } from './events';
export const store = createStore(combineReducers({
users: usersReducer,
events: eventsReducer
}));
这就是商店的样子:
const initialState = {
loggedIn: false,
thisUser: []
}
export function usersReducer(state = initialState, action) {
switch (action.type) {
case 'users/loggedIn':
return { ...state, loggedIn: action.payload }
case 'users/addUser':
return { ...state, thisUser: action.payload[0] }
case 'users/setActivated':
return { ...state, thisUser: { ...state.thisUser, activated: action.payload } }
case 'clearAll':
return {
thisUser: []
}
default:
return state
}
}
有人可以帮助我吗?
I try to transform my Redux state in Redux-Persist but I don't know how to write the code because I use combineReducers.
This is how looks my store:
import { createStore, combineReducers } from 'redux'
import { usersReducer } from './users';
import { eventsReducer } from './events';
export const store = createStore(combineReducers({
users: usersReducer,
events: eventsReducer
}));
And this is how looks a store:
const initialState = {
loggedIn: false,
thisUser: []
}
export function usersReducer(state = initialState, action) {
switch (action.type) {
case 'users/loggedIn':
return { ...state, loggedIn: action.payload }
case 'users/addUser':
return { ...state, thisUser: action.payload[0] }
case 'users/setActivated':
return { ...state, thisUser: { ...state.thisUser, activated: action.payload } }
case 'clearAll':
return {
thisUser: []
}
default:
return state
}
}
Can somebody help me, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需更改
store.js
中的代码即可。先决条件
@reduxjs/toolkit
软件包npm install @reduxjs/toolkit
使用yarn:< code>yarn add @reduxjs/toolkitredux-persist
包npm install redux-persist
using yarn:yarn add redux-persist
在
store.js
文件中进行的更改1. 保留所有减速器
3. 仅保留某些减速器
这里是参考文献:
You can just make changes to your code in
store.js
.Pre-requisite
@reduxjs/toolkit
packagenpm install @reduxjs/toolkit
yarn add @reduxjs/toolkit
redux-persist
packagenpm install redux-persist
yarn add redux-persist
Changes to be made in
store.js
file1. Persist all reducers
3. Persist only certain reducer
Here are the references: