如何在React和Redux中连接mapstatetoprops和mapdispathctoprops

时间:2019-04-29 09:59:39

标签: reactjs redux

运行下面的代码时,我得到了错误。

TypeError: _this.props.signInUser is not a function

代码:

import {signInUser} from '../actions/authAction'

const mapStateToProps = (state) => ({
 auth: state.auth,
 errors: state.errors
});

const mapDispatchToProps = (dispatch) => ({
 hideModal: modelType => dispatch(hideModal())
});

export default connect(
 mapStateToProps,
 mapDispatchToProps,
 null,
 {signInUser}
 )(LoginModal);

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您没有正确将参数传递给connect函数。要连接的第四个参数是options,它在对象内采用以下值

{
  context?: Object,
  pure?: boolean,
  areStatesEqual?: Function,
  areOwnPropsEqual?: Function,
  areStatePropsEqual?: Function,
  areMergedPropsEqual?: Function,
  forwardRef?: boolean,
}

signInUser不是其中之一。我想你的意思是将其传递给mapDisptachToProps

import {signInUser} from '../actions/authAction'

const mapStateToProps = (state) => ({
 auth: state.auth,
 errors: state.errors
});

const mapDispatchToProps = (dispatch) => ({
 hideModal: modelType => dispatch(hideModal()),
 signInUser: (...args) => dispatch(signInUser(...args));
});

export default connect(
 mapStateToProps,
 mapDispatchToProps,
)(LoginModal);
相关问题