使用mergeProps访问mapDispatchToProps

时间:2018-04-13 18:24:03

标签: reactjs react-redux

我需要访问其他调度处理程序在一个调度处理程序中加载的状态。根据{{​​3}}"你打算使用商店中的属性作为调度动作的参数,mergeProps是最干净的选择"

此当前容器代码导致无法定义包含组件所需的道具。我希望有人可以指出我的错误:

const mergeProps = (propsFromState, propsFromDispatch) => {
  return {
    onLeave() {
      const topics = propsFromState.topics;
      return propsFromDispatch.onLeaveWithTopics(topics);
    },
    onReceived(args, kwargs, event) {
      const topics = propsFromState.topics;
      return propsFromDispatch.onReceivedWithTopics(args, kwargs, event, topics);
    },
  };
};

const mapStateToProps = (state) => ({
  topics: state.simpl.topics,
  connectionStatus: state.simpl.connectionStatus,
  errors: state.errors,
  progressComponent: optionsWithDefaults.progressComponent,
});

const mapDispatchToProps = (dispatch) => {
  return ({
...
    onLeaveWithTopics(topics) {
      console.log(`onLeave:: topics: `, topics);
      if (topics) {
        topics.forEach((topic) => {
          dispatch(disconnectedScope(topic));
        });
      }
      return Promise.resolve();
    },
    onReceivedWithTopics(args, kwargs, event, topics) {
      console.log(`onReceived:: args: `, args, `, event: `, event, `, topics: `, topics);
      if (kwargs.error) {
        dispatch(showGenericError(args, kwargs));
      } else {
        const [pk, resourceName, data] = args;
        if (topics) {
          const resolvedTopics = topics.map(
            (topic) => AutobahnReact.Connection.currentConnection.session.resolve(topic)
          );
          resolvedTopics.forEach((topic) => {
            const actions = {
              [`${topic}.add_child`]: addChild,
              [`${topic}.remove_child`]: removeChild,
              [`${topic}.update_child`]: updateScope,
            };
            if (actions[event.topic]) {
              console.log("dispatching: ", actions[event.topic])
              dispatch(actions[event.topic]({ resource_name: resourceName, data, pk }));
            }
          });
        }
      }
    },
  });

1 个答案:

答案 0 :(得分:1)

我可能在这里错了,但你不需要向前传递其他道具:

const mergeProps = (propsFromState, propsFromDispatch) => {
  return {
    ...propsFromState,
    ...propsFromDispatch,
    onLeave() {
      const topics = propsFromState.topics;
      return propsFromDispatch.onLeaveWithTopics(topics);
    },
    onReceived(args, kwargs, event) {
      const topics = propsFromState.topics;
      return propsFromDispatch.onReceivedWithTopics(args, kwargs, event, topics);
    },
  };
};