[英]How to store a complex form object into redux state on submit?
new to React/Redux, I am trying to make it so a user can have as many choices
with different choice
and customAttribute
. React/Redux 的新手,我正在尝试使它成为用户可以通过不同的choice
和customAttribute
拥有尽可能多的choices
。
For now I have a button they can createUI with to dynamically create a new textfield to input another choice
and customAttribute
but am completely stumped on how to store such a thing in redux.现在我有一个按钮,他们可以用createUI中动态创建一个新的文本框输入另一个choice
和customAttribute
但我对如何存储在Redux的这样的事情完全难住了。
I've seen other questions and answers on how to store username and/or password, but have not seen any examples on how to store an object with my full state in it.我已经看到了关于如何存储用户名和/或密码的其他问题和答案,但没有看到任何关于如何存储具有完整状态的对象的示例。
My Component我的组件
class CreateRIG extends Component<any, any> {
constructor(props) {
super(props);
this.state = {
rigName: '',
desc: '',
choices: [{
choice: '',
customAttribute: ''
}]
}
}
createUI() {
return this.state.choices.map((el, i) => (
<div key={i}>
<FormControl>
<Select
id="choice"
name="choice"
onChange={this.handleChange.bind(this, i)}
value={el.choice || ''}
>
<MenuItem value=''>
<em>None</em>
</MenuItem>
<MenuItem value='ID'>ID</MenuItem>
<MenuItem value='PSA'>PSA</MenuItem>
<MenuItem value='ExternalID'>ExternalID</MenuItem>
</Select>
</FormControl>
<div>
<TextField name="customAttribute" label="Attribute"
onChange={this.handleChange.bind(this, i)} value={el.customAttribute || ''} />
))
}
handleSubmit = (event) => {
event.preventDefault();
console.log(this.state);
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<div>
<TextField name="rigName"
value={this.state.rigName}
onChange={event => this.setState({ rigName: event.target.value })}/>
</div>
<div className={styles.spacing}>
<TextField name="description"
onChange={event => this.setState({ desc: event.target.value })}/>
</div>
{this.createUI()}
<Button type="submit"
onClick={this.props.onSubmitForm}>NEXT</Button>
</form>
);
}
}
const mapDispatchToProps = dispatch => {
return {
onSubmitForm: (rigName, desc, choice, customAttribute) => dispatch({ type: 'FORM_DATA'})
}
}
const connectedCreateRIGPage = connect(mapStateToProps, mapDispatchToProps)(CreateRIG);
export { connectedCreateRIGPage as CreateRIG };
export default CreateRIG;
Action.tsx动作.tsx
export const createRIGActions = {
searchInput
};
function searchInput(rigName, desc, choice, customAttribute) {
return {
type: createRIGConstants.STORE_FORM,
rigName,
desc,
choice,
customAttribute
}
}
Reducer.tsx减速器.tsx
const initialState = {
results: []
};
export function createRIGReducer(state = initialState, action) {
switch (action.type) {
case createRIGConstants.STORE_FORM:
return {
...state,
results: state.results
}
// Need to somehow get the data from the form
default:
return state
}
}
How do you store a complex object from a form, into redux state on submit?如何在提交时将复杂对象从表单存储到 redux 状态? Right now my onSubmit is console logging the correct object I want so thats nice现在我的 onSubmit 正在控制台记录我想要的正确对象,这样很好
I believe it is just matter what you pass into dispatch
.我相信你传递给dispatch
。 You can add payload
there as well.您也可以在那里添加payload
。
Try the following:请尝试以下操作:
const mapDispatchToProps = dispatch => {
return {
onSubmitForm: (rigName, desc, choice, customAttribute) => dispatch({ type: 'FORM_DATA', payload: {rigName, desc, choice, customAttribute}})
}
}
Then in your reducer you can access that payload
like the following:然后在您的减速器中,您可以访问该payload
,如下所示:
export default (state=initialState, action) => {
switch(action.type) {
const { payload } = action;
case 'FORM_DATA':
return {
...state,
// here you can use data from payload to update the state
};
Read further here from Redux documentation: Managing Normalized Data从 Redux 文档中进一步阅读: 管理规范化数据
I hope this helps!我希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.