This is function when hit api
function click to triger
const _selectBranch = async () => {
const { user, } = props;
await props.fetchGetBranchList(user.tokenResponse.accessToken, user.customerCode);
const { branchList } = props;
console.log(branchList)
}
this action dispatch
export const getBranchList = (token, code) => async (dispatch) => {
dispatch({
type: BRANCH_LIST_REQ,
});
try {
const res = await apiBranch(token).listBranchGet(code);
if(res.data != null){
dispatch({
type: BRANCH_LIST_SUCCESS,
payload: { data: res.data },
});
}
} catch (error) {
dispatch({
type: BRANCH_LIST_FAILED,
payload: error
});
}
};
my reducers
const initialState = {
branchList: null,
};
export const listNews = (state = { ...initialState }, action) => {
const { payload, type } = action;
switch (type) {
case BRANCH_LIST_REQ: {
return {
...state,
branchList: null
};
}
case BRANCH_LIST_SUCCESS: {
const { data } = payload;
return {
...state,
branchList: data
};
}
default:
return state;
}
};
I succeeded in getting a response, if I use useEffect this code
useEffect(() => {
if(props.branchList){
setStorage("PO_ACTIVE", purchaseOrder);
props.push('/Home/BucketList/', props.branchList.cartId)
}
},[props.branchList]);
but I want to get a response in the process of this code, but if I use this code the response I get is always null
const _selectBranch = async () => {
const { user, } = props;
await props.fetchGetBranchList(user);
const { branchList } = props;
if(branchList != null ){
setStorage("PO_ACTIVE", purchaseOrder);
props.push('/Home/BucketList/', props.branchList.cartId)
}
console.log(branchList)
}