When I get the data on render or when some dependencies change, it updates to the Redux store correctly, but when I console.log it says 'undefined'.
I am developing a booking system and want to get the number of hours booked on a specified date
/// 在组件渲染时获取数据并尝试使用console.log useEffect(() => { axios .get( `http://localhost/healthboyz/index.php/booked/${ formData.doctor_id }/${moment(formData.date).format('YYYY-MM-DD')}` ) .then((res) => setBookedHrs(res.data)) .then(() => console.log(bookedHrs)) .catch((e) => console.log(e));}, [formData.doctor_id, formData.date]);
/// Hours reducer const hoursReducerDefaultState: { bookedHrs: string[]; } = { bookedHrs: [], }; export const hoursReducer = ( state = hoursReducerDefaultState, action: { type: string; data: string[]; } ) => { switch (action.type) { case 'SET_BOOKED_HRS': return [...action.data]; default: return state; } };
The question here actually has to do with how state is set in Redux:
Assume that
hoursReducer
is responsible for the entirehoursReducerDefaultState
, its code is as follows:Then you need to set the Redux state to the state you want to update:
Note: I'm also assuming you want to completely replace the value of
bookedHrs
.