52

我已经使用 redux 编写了一个容器组件,我的实现mapDispatchToProps看起来像这样

const mapDispatchToProps = (dispatch, ownProps) => {
    return {
        onChange: (newValue) => {
            dispatch(updateAttributeSelection('genre', newValue));
            dispatch(getTableData(newValue, ownProps.currentYear));
        }
    }
}

问题是,为了 getTableData 我需要一些其他组件的状态。如何在此方法中访问状态对象?

4

5 回答 5

56

你可以使用 redux-thunk 创建一个单独的 action creator 函数来访问getState,而不是在里面定义函数mapDispatchToProps

function doTableActions(newValue, currentYear) {
    return (dispatch, getState) => {
        dispatch(updateAttributeSelection('genre', newValue));
        let state = getState();
        // do some logic based on state, and then:
        dispatch(getTableData(newValue, currentYear));
    }
}


let mapDispatchToProps = (dispatch, ownProps) => {
    return {
        onChange : (newValue) => {
            dispatch(doTableActions(newValue, ownProps.currentYear))
        }
    }
}

一些不同的方式来组织这些,但这样的事情应该有效。

于 2016-03-07T04:55:01.560 回答
23

可能的方法也是使用mergeProps该合并mapStatemapDispatch允许同时使用两者。

// Define mapState
const mapState = (state) => ({
  needeedValue: state.neededValue
})

// Define mapDispatch
const mapDispatch = (dispatch, ownProps) => {
  return {
    onChange: (newValue, neededValue) => {
      dispatch(updateAttributeSelection('genre', newValue));
      dispatch(getTableData(newValue, ownProps.currentYear, neededValue));
    }
  }
}

// Merge it all (create final props to be passed)
const mergeProps = (stateProps, dispatchProps, ownProps) => {
  return {
    ...stateProps,  // optional
    ...dispatchProps,  // optional
    onChangeWithNeededValue: (newValue) => (
      dispatchProps.onChange(
        newValue,
        stateProps.needeedValue  // <<< here the magic happens
      )
    )
  }
}

// Pass mergePros to connect
const MyContainer = connect(mapState, mapDispatch, mergeProps)(MyComponent);

官方文档:react-redux#connect

大型应用程序可能存在的性能缺陷:Stack Overflow - Redux 中的 Performances and mergeProps

于 2018-01-26T20:20:14.453 回答
7

你可以使用 redux-thunk 来获取状态。编写一个这样的辅助函数:

const getState = (dispatch) => new Promise((resolve) => {
  dispatch((dispatch, getState) => {resolve(getState())})
})

您可以在异步函数或生成器函数中使用它:

const mapDispatchToProps = (dispatch, ownProps) => {
  return {
    async someFunction() {
      const state = await getState(dispatch)
      ...
    }
  }
}
于 2017-04-21T02:39:28.497 回答
1

您可以尝试使用:

redux-named-reducers

这允许您在代码中的任何位置获取状态,如下所示:

const localState1 = getState(reducerA.state1)
const localState2 = getState(reducerB.state2)

同样在 mapDispatchToProps 中:

const mapDispatchToProps = dispatch => {
  return {
    onClick: () => {
      dispatch(someAction(getState(moduleA.state1)));
    }
  };
};
于 2018-03-12T09:18:35.643 回答
1

如果您使用过 Thunk 中间件,那么您可以将辅助函数写入 Action.Js

export const getSearchedText =  () => (dispatch, getState) => {
    const { app } = getState();
    return app.searchedText;
}

如果你用过容器设计模式,你的属性容器应该在下面

容器.js

export const mapDispatchToProps = (dispatch, ownProps) => {
    return {
             setSearch: search => {
                 var searchedText = dispatch(getSearchedText());
             }
}
于 2018-10-02T20:58:34.517 回答