反应,API网址参数更改后,Redux会调用一个操作

时间:2019-02-14 13:10:50

标签: reactjs redux

我遇到了无法解决的问题。 给定具有url参数的此API调用。 参数更改后,组件应立即获取新数据。 因此,如果日期更改,则redux应该使用新日期再次调用此函数并显示新数据。

我的代码在 action.js 中:

 export const getSingleDoctorAppointments = (clinicId, doctorId, date, 
  appointmentTypeId) => dispatch => {
 if (date !== null && date !== undefined) {
    const url = `${apiAddress}DoctorAppointment?clinicId=${clinicId}&doctorId=${doctorId}&date=${date.getDate() +
        '/' +
        (date.getMonth() + 1) +
        '/' +
        date.getFullYear()}&appointmentTypeId=${appointmentTypeId}&insuranceType=&overview=true`;

    axios
        .get(url)
        .then(res =>
            dispatch({
                type: GET_SINGLE_DOCTOR_APPOINTMENTS,
                payload: res.data
            })
        )
        .then(
            dispatch({
                type: PROFILE_LOADING,
                payload: 'Loading Profile'
            })
        )
        .catch(err =>
            dispatch({
                type: GET_SINGLE_DOCTOR_APPOINTMENTS,
                payload: null
            })
        );
}
};

此功能后,调用我想使用新参数获取新数据:

export const getSelectedDate = date => dispatch => {
dispatch({
    type: GET_SELECTED_DATE,
    payload: date
});
};

我在组件本身内部尝试了componentWillRecieveProps。 没有工作。 我希望这是可以理解的。 我想这是因为异步和同步调用。

0 个答案:

没有答案
相关问题