我有以下设置,在构造函数中使用.bind(this)的所有方法。因此,用户在输入中输入用户名和密码并在状态中更新,并且组件被重新呈现并且新更新的状态this.props.username
和this.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>
)
}
}
谢谢
答案 0 :(得分:1)
您不需要将参数传递给_handleRegister
_handleRegister() {
const { username, password, registerUser } = this.props;
registerUser(username, password);
}
附加提示:您可以通过执行以下操作来跳过return关键字:
render() (
<View>
...
</View>
)