我正在尝试将具有多个属性和值的有效负载分配给reducer
我的调度程序组件,例如:
class Example extends Component {
render() {
return (
<React.Fragment>
{//here i have a form to submit name and value}
</React.Fragment>
);
}
}
const mapDispatchToProps = () => {
return dispatch => {
dispatch({ type: 'NINJA', payload: { name: 'aaaa', value: 5 } });
}
};
export default connect(null, mapDispatchToProps)(Example);
我的减速器:
const intialstate = {}
const reducer = (state = intialstate, action) => {
console.log("inside reducer");
switch (action.type) {
case 'NINJA':
return {
...state,
name: action.payload.name,
value: action.payload.value
}
};
return state;
}
export default reducer;
一旦该操作被发送,我会收到一条错误消息:
对象无效,不能作为React子对象(找到:带有键{name,value}的对象)。如果要渲染子级集合,请改用数组。
并帮助我将具有名称和值之类的属性的完整对象“ initialState”放入另一个显示它们的组件中。