Reactjs 在表单提交上调度操作,以将对象添加到reducer中的数组
我试图以一种模式提交一份表格,在我的状态中添加时间和事件。我的样本数据是:Reactjs 在表单提交上调度操作,以将对象添加到reducer中的数组,reactjs,redux,Reactjs,Redux,我试图以一种模式提交一份表格,在我的状态中添加时间和事件。我的样本数据是: { date: "Sat 2nd", enjoyments: ['Football', 'Rugby'], achievements: [{'Tennis', '1100am'}, {'Football', '1200pm'], id: 1 }, 我有我的表格: submitForm(){ this.props.onChange(this.props.day
{
date: "Sat 2nd",
enjoyments: ['Football', 'Rugby'],
achievements: [{'Tennis', '1100am'}, {'Football', '1200pm'],
id: 1
},
我有我的表格:
submitForm(){
this.props.onChange(this.props.dayId, e.label, )
}
render() {
console.log(this.props)
var options = [
{ value: 1, label: 'Play Music' },
{ value: 2, label: 'Football' }
];
var hourSelect = [
{ value: 0, label: '00' },
{ value: 1, label: '01' },
{ value: 2, label: '02' },
{ value: 3, label: '03' },
{ value: 4, label: '04' },
{ value: 5, label: '05' },
{ value: 6, label: '06' },
{ value: 7, label: '07' },
{ value: 8, label: '08' },
{ value: 9, label: '09' },
{ value: 10, label: '10' },
{ value: 11, label: '11' },
{ value: 12, label: '12' },
{ value: 13, label: '13' },
{ value: 14, label: '14' },
{ value: 15, label: '15' },
{ value: 16, label: '16' },
{ value: 17, label: '17' },
{ value: 18, label: '18' },
{ value: 19, label: '19' },
{ value: 20, label: '20' },
{ value: 21, label: '21' },
{ value: 22, label: '22' },
{ value: 23, label: '23' }
];
var minSelect = [
{ value: 0, label: '00' },
{ value: 15, label: '15' },
{ value: 30, label: '30' },
{ value: 45, label: '45' }
];
return (
<span >
<ModalWrapper
onRequestClose={this.props.closeModal}
style={this.props.customStyles}
contentLabel="Modal" >
<h2>Add Achievement</h2>
<Select
name="form-field-name"
value="one"
options={options}
/>
<Select
name="form-field-name"
value="one"
options={hourSelect}
/>
<Select
name="form-field-name"
value="one"
options={minSelect}
/>
<a href="#" onClick="this.props.closeModal" >Submit</a>
</ModalWrapper>
</span>
)
我的减速机目前只增加了一个成就数组,我如何更改我的减速机来为一个对象数组增加一个时间
case ADD_ACHIEVEMENT:
return state.map(item => {
if (item.id === action.id) {
return Object.assign({}, item, {
achievements: [
...item.achievements,
action.text,
]
});
}
return item;
});
获取数据时出错:
11 | date: "Sat 2nd",
12 | enjoyments: ['Football', 'Rugby'],
> 13 | achievements: [{'Tennis', '1100am'}, {'Football', '1200pm'}],
| ^
14 | id: 1
15 | },
您需要在
{}
<a href="#" onClick={this.props.closeModal} >Submit</a>
您需要在{}内为onClick提供函数。谢谢如何更改我的reducer以将对象添加到包含时间的数组中?请检查答案:)谢谢我的数据有错误,我可以像使用数组中的对象一样使用对象吗?这不是对象;对象具有键和值,例如,
{netsing:'1100am'}
。您正在混合对象和数组表示法。谢谢,但我在问题中添加了一个错误,您能帮忙吗?
<a href="#" onClick={this.props.closeModal} >Submit</a>
case ADD_ACHIEVEMENT:
return state.map(item => {
if (item.id === action.id) {
return {...item,
achievements: [
...item.achievements,
action.text,
],
date: action.time
};
}
return item;
});