基于onChange事件值更改redux状态

问题描述 投票:1回答:2

我正在使用redux,并且我的状态中包含空字符串值。每个空键都必须使用输入onChange事件来填写。假设我要更改“ business_selected”键,该函数应该是什么样?这是我的状态:


const initialState = {

        user: {},
        applications: [
            {
                companyFlow: {
                    stage1: {
                        business_activity: "",
                        business_selected: ""
                    },

                    stage2: {
                        legal_name: "",
                        registration_number: "",
                        business_website: "",
                        incorporation_date_text: "",
                        legal_form: ""
                    }
                }
            }
        ]
};

export default function changeInput(state = initialState, action) {
  switch (action.type) {
    case CHANGE_INPUT:
      return {
        // ??????
      };
    default:
      return state;
  }
}
redux
2个回答
1
投票

onChnage值应以本地状态而不是Redux状态存储。理想情况下,仅全局值应以redux状态存储。


0
投票

通常onChange是使用component本身的本地状态来处理的,但是如果要在redux中进行操作,则可以执行类似的操作。

export default function changeInput(state = initialState, action) {
  switch (action.type) {
    case CHANGE_INPUT:
      return {
       ...state,
       applications: state.applications.map(application => {
        return Object.assign({}, application, {
         ...application,
         companyFlow: {
          ...application.companyFlow,
          stage1: {
            ...application.companyFlow.stage1,
            business_selected: action.payload
          }
         }
        }
       return application
      });
    default:
      return state;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.