javascript - 如何在 React 中将数据从 Form 传递到 mapDispatchToProps
问题描述
我是新手,试图构建一个登录表单,能够从表单中提取用户名和密码。但是我在尝试将其传递给 mapDispatchToProps 以将其分派到后端时遇到了麻烦。
我将如何从“值”获取用户名和密码以实际将其传递给 mapDispatchToProps?
我收到一条错误消息,提示无法在函数外部访问“onAuth”。
这是代码片段:
const NormalLoginForm = (props) => {
const onFinish = values => {
const onAuth = (values.username, values.password) // This is where the error is occuring
console.log('Success:', values);
};
const onFinishFailed = errorInfo => {
console.log('Failed:', errorInfo);
};
return (
<Form
{...layout}
name="basic"
initialValues={{
remember: true,
}}
onFinish={onFinish}
onFinishFailed={onFinishFailed}
>
<Form.Item
label="Username"
name="username"
rules={[
{
required: true,
message: 'Please input your username!',
},
]}
>
<Input />
</Form.Item>
<Form.Item
label="Password"
name="password"
rules={[
{
required: true,
message: 'Please input your password!',
},
]}
>
<Input.Password />
</Form.Item>
<Form.Item {...tailLayout}>
<Button type="primary" htmlType="submit">
Submit
</Button>
<NavLink
style={{marginRight: '10px'}}
to='/signup/'> Signup
</NavLink>
</Form.Item>
</Form>
);
};
const mapStateToProps = (state) => {
return {
loading: state.loading,
error: state.error
}
}
const mapDispatchToProps = dispatch => {
return {
onAuth: (username, password) => dispatch(actions.authLogin(username, password))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(NormalLoginForm);
解决方案
看起来 mapDispatchToProps 已经正确设置,您只需要调用它创建的函数。
const onFinish = values => {
props.onAuth(values.username, values.password);
}
推荐阅读
- python - 将 scikit-learn 决策树解析为 JSON
- android - 不同设备上的负边距
- amazon-web-services - 如何将我的 subdomin 连接到 Route 53 记录集到不同的端口?
- python - 如何调用函数并更改一些参数?
- flutter - Flutter firebase OnLaunch 通知有效负载缓存
- android - layout_constraintHorizontal_bias 和 layout_constraintVertical_bias
- c++ - 二进制 '<<' :没有找到采用 const T (模板)类型的右手操作数的运算符
- php - Laravel 用户有很多用户类
- node.js - 相同的查询给出不同的结果(nodejs、postgres、日期)
- python - 找不到满足要求的版本 torch~=1.4.0(来自 syft)(来自版本:0.1.2、0.1.2.post1、0.1.2.post2)