简体   繁体   English

React-Redux 为 state 添加新值

[英]React-Redux add new value to the state

I am having a two step form in react.我有一个两步反应的形式。 The first step of the form we ask some information to the user and then I add it to the state. The second step of the form I ask some more information to the user and add it to the state, so instead of appending the information that was asked on step 2 of the form, it overrides the state, so the state now only the info that was asked in the step 2 of the form.表格的第一步我们向用户询问一些信息,然后我将其添加到 state。表格的第二步我向用户询问更多信息并将其添加到 state,所以不是附加信息在表格的第 2 步被询问时,它会覆盖 state,因此 state 现在只有在表格的第 2 步中询问的信息。 How can I add the have both the information together.我怎样才能将这两种信息加在一起。 When i try to...state it gives me error as state is not iterable.当我尝试...state 时,它给我错误,因为 state 不可迭代。

const infoReducer = (state = {}, action) => {
switch(action.type) {
    case 'STEP_1':
        return action.payload
    case 'STEP_2':
        return action.payload
    default:
        return state
}

} }

first, copy the current state data and add a new action payload as second parameter首先,复制当前的 state 数据并添加一个新的动作负载作为第二个参数

 case 'ADD_Data': return {...state,action.payload}

check this out看一下这个

 import { ActionTypes } from "../../Constant/ActionType"; const initState = { Auth: {}, }; const AuthReducer = (state = initState, action) => { switch (action.type) { case ActionTypes.AuthUser: return {...state, Auth: action.payload, }; default: return state; } }; export default AuthReducer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM