gpt4 book ai didi

javascript - 如何从提交表单中获取输入值并存储在 redux 存储变量中?

转载 作者:行者123 更新时间:2023-11-30 14:35:04 25 4
gpt4 key购买 nike

我已经制作了银行账户提交表格。我想将表单中输入的数据保存到 redux 存储中。如何从表单中获取输入值并将其存储在 redux 存储变量中?

我的 client.js 文件有 redux 存储,而 form.js 是我需要从中获取输入值的组件。客户端.js:

import { combineReducers, createStore } from 'redux';

const addUserReducer = (state={}, action) => {
switch(action.type){
case: "CHANGE_FIRSTNAME"{
state = {...state, firstname:action.payload }
break;
}
case: "CHANGE_LASTNAME"{
state = {...state, lastname:action.payload }
break;
}
case: "CHANGE_EMAILID"{
state = {...state, emailid:action.payload }
break;
}
case: "CHANGE_IBAN"{
state = {...state, iban:action.payload }
break;
}
case: "CHANGE_BANKNAME"{
state = {...state, bankname:action.payload }
break;
}
}
return state;
}

const reducers = combineReducers({
addUser:addUserReducer
})


const store = createStore(reducers);

store.subscribe(() => {
console.log('Store changed', store.getState());
})

store.dispatch({type: "CHANGE_FIRSTNAME", payload:"Will"});
store.dispatch({type: "CHANGE_LASTNAME", payload:"Groot"});
store.dispatch({type: "CHANGE_EMAILID", payload:"xyz@gmail.com"});
store.dispatch({type: "CHANGE_IBAN", payload:3234243242});
store.dispatch({type: "CHANGE_BANKNAME", payload:"XYZ"});

表单.js:

import React, { Component } from "react";
import "./form.css";

class Form extends Component {
render() {
return (
<div>
<div id="center">
<form>
<div className="form-group">
<label for="firstname">First Name:</label>
<input type="firstname" className="form-control" id="firstname" />
</div>

<div className="form-group">
<label for="lastname">Last Name:</label>
<input type="lastname" className="form-control" id="lastname" />
</div>

<div className="form-group">
<label for="email">Email address:</label>
<input type="email" className="form-control" id="email" />
</div>

<div className="form-group">
<label for="bankacc">IBAN:</label>
<div id="deletebank" className="items">
<input type="bankacc" className="form-control" id="bankacc" />
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash" />
</button>
</div>
</div>

<div className="form-group">
<label for="bankname">Bank Name:</label>
<input type="bankname" className="form-control" id="bankname" />
</div>

<div className="form-group">
<button type="button" className="btn addbank">
+ Add bank account
</button>
</div>

<div className="form-group">
<button type="button" class="btn btn-success">
Submit
</button>
</div>
</form>
</div>
</div>
);
}
}

export default Form;

我的表格截图:

enter image description here

最佳答案

我建议使用 react-redux 将您的组件与 redux store 连接起来,但是没有它仍然可行:

创建将更新商店中特定变量的 Action 创建者:

import { bindActionCreators } from "redux";

const updateFirstName = name => ({ type: "CHANGE_FIRSTNAME", payload: name });
const updateLastName = lastName => ({
type: "CHANGE_LASTNAME",
payload: lastName
});
const updateEmail = email => ({ type: "CHANGE_EMAILID", payload: email });
const updateIban = iban => ({ type: "CHANGE_IBAN", payload: iban });
const updateBankName = bankName => ({
type: "CHANGE_BANKNAME",
payload: bankName
});

现在用 dispatch 绑定(bind)你的 action creators,所以调用 actionCreators.updateFirsName('something') 实际上会向 store 派发一个 action。

export const actionCreators = bindActionCreators(
{
updateFirstName,
updateLastName,
updateEmail,
updateIban,
updateBankName
},
store.dispatch
);

现在您只需要在输入发生变化时调用每个存储更新函数:

import React, { Component } from "react";
import "./form.css";
import { actionCreators } from "/path/to/store";

class Form extends Component {
render() {
return (
<div>
<div id="center">
<form>
<div className="form-group">
<label for="firstname">First Name:</label>
<input
type="firstname"
className="form-control"
id="firstname"
onChange={e => actionCreators.updateFirstName(e.target.value)}
/>
</div>

<div className="form-group">
<label for="lastname">Last Name:</label>
<input
type="lastname"
className="form-control"
id="lastname"
onChange={e => actionCreators.updateLastName(e.target.value)}
/>
</div>

<div className="form-group">
<label for="email">Email address:</label>
<input
type="email"
className="form-control"
id="email"
onChange={e => actionCreators.updateEmail(e.target.value)}
/>
</div>

<div className="form-group">
<label for="bankacc">IBAN:</label>
<div id="deletebank" className="items">
<input
type="bankacc"
className="form-control"
id="bankacc"
onChange={e => actionCreators.updateIban(e.target.value)}
/>
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash" />
</button>
</div>
</div>

<div className="form-group">
<label for="bankname">Bank Name:</label>
<input
type="bankname"
className="form-control"
id="bankname"
onChange={e => actionCreators.updateBankName(e.target.value)}
/>
</div>

<div className="form-group">
<button type="button" className="btn addbank">
+ Add bank account
</button>
</div>

<div className="form-group">
<button type="button" class="btn btn-success">
Submit
</button>
</div>
</form>
</div>
</div>
);
}
}

export default Form;

关于javascript - 如何从提交表单中获取输入值并存储在 redux 存储变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50549982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com