调度动作时未调用redux connect mapStateToProps

时间:2019-05-10 08:42:08

标签: javascript reactjs redux react-redux

  

解决方案(已更新):

我认为任何动作都会导致react-redux-connect调用mapState函数,但是当某个动作没有任何改变时,情况并非如此。

我有一个localStorage模块,该模块可以调度操作,但不更改状态,而是您将写入localStorage。该模块具有在容器中使用的选择器,但是直到状态真正改变时它们才被调用,因此只有在分派了另一个会改变状态的动作之后,UI才能正确显示。

  

问题

当我将商店放在窗口(window.store=store上时,在mapStateToProps中添加一个console.log,然后在控制台中我调度一个动作:store.dispatch({type:'some action'})然后mapStateToProps的console.log执行不显示。

我确实记住了结果,但是mapStateToProps应该称为see here

完整代码为here,并运行示例here(您可以通过单击屏幕右下方的“控制台”链接来打开控制台)。

package.json

store.js:

import { createStore } from 'redux';
export default (initialState, reducer) => {
  const store = createStore(
    reducer,
    initialState,
    window.__REDUX_DEVTOOLS_EXTENSION__ &&
      window.__REDUX_DEVTOOLS_EXTENSION__()
  );
  window.store = store;
  return store;
};

app.js

import React from 'react';
import { connect } from 'react-redux';
import './App.css';
import createStore from './store';
import { Provider } from 'react-redux';
import initCounter from './components/Counter';
import {
  createWrapper,
  memoize,
} from './components/@common';
const COUNTER = 'COUNTER';
const selectCounterState = state => state.myCounter;
const counter = initCounter({
  actionWrapper: createWrapper(COUNTER, 'counter1'),
  selectors: { myState: selectCounterState },
  connect,
  memoize,
});
const initialState = {
  myCounter: counter.initialState,
};
const reducer = (state = initialState, action) => {
  if (action.emittedBy === COUNTER) {
    return {
      ...state,
      myCounter: counter.reducer(
        selectCounterState(state),
        action.payload
      ),
    };
  }
  return state;
};
const store = createStore(initialState, reducer);
const Counter = counter.container;
const App = () => (
  <Provider store={store}>
    <Counter id="counter1" parentId={[]} />
  </Provider>
);

export default App;

组件/计数器/索引:

import component from './component';
const INCREASE = 'INCREASE';
const reducer = (state, action) => {
  if (action.type === INCREASE) {
    return { ...state, count: state.count + 1 };
  }
  return state;
};
const makeState = memoize =>
  memoize((id, parentId, { count }) => ({
    id: parentId.concat(id),
    parentId,
    count,
  }));
const mapStateToProps = ({ myState }, memoize) => () => {
  const newState = makeState(memoize);
  return (state, ownProps) =>
    console.log('in map state to props', new Date()) ||
    newState(
      ownProps.id,
      ownProps.parentId,
      myState(state)
    );
};

export default ({
  actionWrapper,
  selectors,
  connect,
  memoize,
}) => {
  const actions = {
    increase: ({ id }) =>
      actionWrapper({
        type: INCREASE,
        id,
      }),
  };
  const container = connect(
    mapStateToProps(selectors, memoize),
    actions
  )(component);
  return {
    container,
    reducer,
    initialState: { count: 0 },
  };
};

components / counter / component.js:

import React from 'react';

export default props => (
  <div>
    <button onClick={() => props.increase(props)}>
      add
    </button>
    {props.count}
  </div>
);

2 个答案:

答案 0 :(得分:1)

您的示例代码笔工作正常,您只需要触发一个超出您的顶级警卫并且具有预期结构的操作即可,以免引起任何后续错误:

将其发布到您的Codepen控制台中:

store.dispatch({emittedBy: "COUNTER", type: "COUNTER -> INCREASE", id: "counter1", payload: {type: "INCREASE", id: ["counter1"]}})

答案 1 :(得分:0)

此问题是由于我有一个localStorage模块执行的,但该模块执行调度操作但未更改状态,而是将其写入localStorage。

该模块具有选择器,这些选择器将获取正确的数据,并且容器将使用它们来构建正确的状态,但是由于分派的操作并未更改redux存储中的状态,因此react-redux会跳过调用我的mapState函数(可能会记住提供商)。

解决方案是让根缩减器返回新的状态引用{...state},以便任何操作都会导致调用mapState函数。