React Native + Redux:如何正确地将道具传递给动作来电者?

时间:2016-09-22 05:42:33

标签: javascript reactjs react-native redux react-redux

我有以下设置,在构造函数中使用.bind(this)的所有方法。因此,用户在输入中输入用户名和密码并在状态中更新,并且组件被重新呈现并且新更新的状态this.props.usernamethis.props.password被传递下来。然后,我想在点击“注册”按钮后将它们传递给名为this.props.registerUser()的动作创建者。

所以我想知道,将新更新的道具传递给动作创作者的正确做法是什么?

例如

_handleRegister(this.props.username, this.props.password)然后是this.props.registerUser(this.props.username, this.props.password)吗?或者只是this.props.registerUser(this.props.username, this.props.password)?或者是this._handleRegister(this.props.username, this.props.password)和前者的组合?

  _handleRegister() {
    this.props.registerUser()
  }

  render() {

    return (
     <View> 
      <TextInput
        placeholder={'Username'}
        onChangeText={...}
        value={this.props.username}
      />
      <TextInput
        placeholder={'Password'}
        onChangeText={...}
        value={this.props.password}
      />
      <TouchableHighlight onPress={this._handleRegister}>
        <Text>Register</Text>
      </TouchableHighlight>           
     </View>
    )
  }
}

谢谢

1 个答案:

答案 0 :(得分:1)

您不需要将参数传递给_handleRegister

_handleRegister() {
    const { username, password, registerUser } = this.props;
    registerUser(username, password);
}

附加提示:您可以通过执行以下操作来跳过return关键字:

render() (
  <View>
     ...
  </View>
)