React Native + Redux:如何正确地将道具传递给动作调用者?
React Native + Redux: How to properly pass in props to action callers?
我有以下设置,所有方法都在构造函数中带有 .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>
)
}
}
谢谢
你不需要传递参数给_handleRegister
_handleRegister() {
const { username, password, registerUser } = this.props;
registerUser(username, password);
}
额外提示:您可以通过以下操作跳过 return 关键字:
render() (
<View>
...
</View>
)
我有以下设置,所有方法都在构造函数中带有 .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>
)
}
}
谢谢
你不需要传递参数给_handleRegister
_handleRegister() {
const { username, password, registerUser } = this.props;
registerUser(username, password);
}
额外提示:您可以通过以下操作跳过 return 关键字:
render() (
<View>
...
</View>
)