REACT REDUX - 使用标准化数据更新记录

时间:2017-04-18 01:41:37

标签: reactjs redux normalizr

这是我在reducer中的initialState

const initialState = {
    ids:[],
    periodData: {}
}

初次加载后这是我的商店 enter image description here

我想更新备注 periodID:3477dac1-d14a-4aef-b544-d8945b2442b9“从”test“到”test1“

这是我要更新的行动:

export function updatePeriod(data){
    return function(dispatch){

        let formData = new FormData();
        formData.append("periodID", data.periodID);
        formData.append("periodName", data.periodName);
        formData.append("startDate", fecha.format(data.startDate, 'YYYY-MM-DD'));
        formData.append("endDate", fecha.format(data.endDate, 'YYYY-MM-DD'));
        formData.append("remark", data.remark);
        formData.append("isActive", 1);

        dispatch({type: PERIOD_FETCHING}); //Loading Indicator Appeared
        axios.post(`${ROOT_URL}/api/setup/createPeriod`,formData,
        {
            headers:{authorization:`Bearer `+localStorage.getItem('laravel_user_token')}
        }
        ).then(response => {
            let result = {
                "periodID": data.periodID,
                "periodName": data.periodName,
                "startDate": fecha.format(data.startDate, 'YYYY-MM-DD'),
                "endDate": fecha.format(data.endDate, 'YYYY-MM-DD'),
                "remark": data.remark,
                "isActive": '1'
            }
            console.log(normalize(result, schema.periodSchema));
            dispatch({type: PERIOD_UPDATE,
                payload: normalize(result, schema.periodSchema)
            });
            dispatch({type: PERIOD_FETCHED}) //Loading Indicator Disappeared
        }).catch((e)=>{
            console.log(e)
             dispatch({type: PERIOD_FETCH_ERROR,
            });
        });
    }
}

这是

的结果
  

console.log(normalize(result,schema.periodSchema));

enter image description here

我仍然对我的reducer如何在我的商店中更新(PERIOD_UPDATE)ids和periodData感到困惑。

0 个答案:

没有答案