I have a getFileAeroexpress function in my service, which makes a request to the server and saves the response to the store:
getFileAeroexpress = async (data) => {
this.setLoading(true);
try {
const response = await Api.Trip.getFileAeroexpress(data);
store.dispatch({
type: ACTIONS.TRIP.GET_TICKET_FIELD_ID,
payload: response,
});
this.setLoading(false);
} catch (error) {
this.setLoading(false);
}
};
Next, getFileAeroexpress is used in the AeroExpress component.
I have a state in my AeroExpress component:
const [ticketFileId, setTicketFileId] = useState(null);
Below is a subscription to the store:
const updateTripData = ({
employeesList: employees,
customFile: voucher,
ticketFileId: fileId,
aeroexpressLoading: loading,
}) => {
console.log(fileId) // here fileId is the value that is in the store
setEmployeesList(employees);
setCustomFile(voucher);
setTicketFileId(fileId);
setAeroexpressLoading(loading);
};
useEffect(() => {
const unsubscribe = tripService.subscribe(updateTripData);
return () => {
unsubscribe();
};
}, []);
Below there is a handleConfirm function which is used as onClick
const handleConfirm = () => {
// other code
getFileAeroexpress(transactionParam);
console.log(ticketFileId); here ticketFileId is null
// other code
addTrip(stateToSave);
onConfirm(item);
} else {
scrollToErrMessage();
setValidation(prev => ({
...prev,
...validationErrors,
}));
}
};
return(
<Button
onClick={ handleConfirm }
label={ LABELS.CONFIRM }
inValidMessage={ message }
/>
)
The getFileAeroexpress function makes a request and puts into the store what came in the response. I subscribe to a store in my component. Inside updateTripData, the fileId parameter is equal to what came in the getFileAeroexpress response. But for some reason ticketFileId in handleConfirm is null. Why? After running getVoucherAeroexpress, I need to get the current ticketFileId from the store. How can this be done?
homie43 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.