简体   繁体   English

Redux-重置状态

[英]Redux - reset state

I'm trying to implement a redux reset state action based off a suggestion from Dan Abramov here . 我正在尝试根据Dan Abramov的建议在此处实施redux重置状态操作。 I've setup my reducers and store like so: 我已经设置了我的减速器并像这样存储:

Index.js: Index.js:

import {applyMiddleware,createStore} from 'redux';
import combineReducers from './reducers/index';
import {wrapStore,alias} from 'react-chrome-redux';
import thunk from 'redux-thunk';
import aliases from './aliases/aliases';

const combineAppReducers = ( state, action ) => {
  if ( action.type === 'LOG_OUT' ) {
    state = undefined;
  }

  return combineReducers(state, action)
}

const middlewares = [alias(aliases), thunk];

const store = createStore(combineAppReducers,applyMiddleware(...middlewares));

wrapStore(store, {
  portName: 'example'
});

Reducers.js: Reducers.js:

import {combineReducers} from 'redux';
import userAuthReducer from './userAuthReducer';
import manageTeamsReducer from './manageTeamsReducer';

function lastAction(state = null, action) {
  return action;
}

export default combineReducers({
  userAuthReducer,manageTeamsReducer,lastAction
});

It looks as though I have set everything up correctly but the app is not resetting the state, can anyone spot where i've gone wrong? 似乎我已正确设置了所有内容,但应用程序未重置状态,有人可以发现我出了错吗?

Here is another article on it which I more closely followed: 这是另一篇文章,我对此进行了更密切的关注:

https://medium.com/@agungsantoso/how-to-reset-the-state-of-a-redux-store-7f9d85b190bc https://medium.com/@agungsantoso/how-to-reset-the-state-of-a-redux-store-7f9d85b190bc

It is work fine. 很好 You can check my snippet. 您可以查看我的代码段。 Can you upload an error log? 您可以上传错误日志吗?

 function lastAction(state = null, action) { return action; } function counter(state = 0, action) { if ( action.type === 'INC' ) { return state+1; } return state; } const combineAppReducers = Redux.combineReducers({ lastAction, counter }); const combineReducers = ( state, action ) => { if ( action.type === 'LOG_OUT' ) { state = undefined; } return combineAppReducers(state, action) } const store = Redux.createStore(combineReducers); const root = document.createElement('ul'); document.body.append(root); let unsubscribe = store.subscribe(() =>{ const li = document.createElement('li'); li.append(store.getState().counter); root.append(li); }) store.dispatch({type: 'INC'}); store.dispatch({type: 'INC'}); store.dispatch({type: 'INC'}); store.dispatch({type: 'LOG_OUT'}); store.dispatch({type: 'INC'}); 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.7.2/redux.js"></script> </head> <body> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM