首页 > 解决方案 > React redux 数据未成功通过

问题描述

我正在尝试将反应数据传递给 redux,但它似乎并没有成功通过,即使我编写的代码与教程完全一样。

以下是相关功能:

在 components/login-form.js

testFunction(text) {
    console.log("The text entered is: ", text); //this reflects successful text entry
    this.props.authInputChange({'field': 'email', 'value:': text});
  }

const mapStateToProps = state => {
  console.log("The state is: ", state); //this does not get updated

  return {
    email: state.auth.email,
    password: state.auth.password
  }
};

export default connect(mapStateToProps, { authInputChange, login })(LoginForm);

在行动/index.js

export const authInputChange = ({ field, value }) => {
    console.log("The value entered is: ", value); //this value is empty
    return {
        type: 'AUTH_INPUT_CHANGE',
        payload: { field, value }
    };
}

现在,即使我完全按照教程进行了操作(除了我自己添加的 console.log 语句),我怀疑讲师犯了一个错误,这就是为什么有些事情没有通过它应该在的地方。但我不知道它会是什么,因为我是 redux 的菜鸟。

谢谢您的帮助!

标签: react-nativereduxreact-redux

解决方案


回顾这一行:

authInputChange({'field': 'email', 'value:': text})

您设置value:textnot value。所以你要:

authInputChange({'field': 'email', 'value': text})

另外,我假设您的减速器设置正确。

旁注:这也是我不想在对象键周围加上引号的原因。如果你没有在周围加上引号,value:你会立即看到这个错误。


推荐阅读