行动之间的Redux状态发生变化

时间:2018-01-28 22:31:35

标签: javascript reactjs redux react-redux redux-persist

我正在使用redux-logger查看redux状态的上一个 。我的问题是我的状态似乎在调度动作之前发生了变化:

此处显示之后状态如何更新(placementName属性已正确分配)

Here it shows how the **after** state does update (the placementName property is correctly assigned)

Here it shows that the **prev** state changes with the placementName property, so the previous **next** state is NOT the same as the current **prev** state, and they always should be

此处显示 prev 状态随placementName属性而变化,因此之前的 next 状态与当前 prev 不同国家,他们应该永远......

如果我在Chrome控制台中打开和关闭对象,则重新评估它,然后之前的下一个状态与当前状态的 prev 相同。我不知道改变了什么!我无法使用之前的数据更新redux状态,因为它总是被覆盖。

我正在使用redux-persist来保持用户使用React进行服务器端渲染:

const { persistor, store } = configureStore();

const render = Component => {
  ReactDOM.render(
    <AppContainer>
      <Provider store={ store }>
        <PersistGate persistor={ persistor }>
          <BrowserRouter>
            <Component />
          </BrowserRouter>
        </PersistGate>
      </Provider>
    </AppContainer>,
    document.getElementById('root')
  );
};

configureStore():

import { persistStore } from 'redux-persist';
  // Add redux-persist's configuration
  const config = {
    transforms: [immutableTransform()],
    key: 'root',
    storage
  };

const reducer = combineReducers(rootReducer);
const persistedReducer = persistReducer(config, reducer);

// Export the persistor as well
  let persistor = persistStore(store);

  return { persistor, store };

我不确定redux-persist是否与此问题有关...

修改

这是我的减速机

[SAVE_INPUTS]: (state, {toSaveInputs}) => {
    const savedInputs = state.get('savedInputs');
    console.log("\n\nREDUX!\n\n");
// This line here prints the 'new' state before even returning it...
    console.log("Saved Inputs State: ", savedInputs);
    console.log("To save Inputs: ", toSaveInputs);

    const newSavedInputs = [...savedInputs, toSaveInputs];

    return state.merge(Map({
      savedInputs: newSavedInputs
    }));
  },

结果应该是toSaveInputs被添加到redux状态的savedInputs数组中,但它的作用是用新输入替换先前状态的数组然后在添加新的toSaveInputs

0 个答案:

没有答案
相关问题