我认为我的代码是正确的,它会检查 QueryString 是否未定义。如果是,则“thePath”从配置文件中获取值。如果 QueryString 有一个值(这意味着不再未定义),那么让 'thePath' 的值是 QueryString。它可以工作,但它不会在函数的 return(dispatch) 部分中更新.. 为什么?
该函数用于根据 axios 的结果设置不同的操作,最终成为 reducer 和 redux store 的一部分。
export let fetchingBook = (QueryString) => {
let thePath;
if (QueryString === undefined) {
thePath = process.env.REACT_APP_GATEWAY_BOOK_PATH
}
else
if (QueryString !== undefined) {
thePath = QueryString
}
console.log("thePath : " + thePath);
return (dispatch) => {
console.log("came to return dispatch first time, does not come here the second time.. why?");
dispatch(fetch_BOOK_Request())
axios.get(thePath)
.then(response => {
const BookDATA = response.data
dispatch(fetch_BOOK_Success(BookDATA))
})
.catch(error => {
const ErrorMsg = error.message
dispatch(fetch_BOOK_Failure(ErrorMsg))
})
}
}
更新:它在哪里被派出
const dispatching = dispatch => {
return {
// following is for another reducer
goFindByPlanNumber__functionalProp: planNum => dispatch({ type: "SEARCH_BY_PLAN_NUMBER", txt: planNum }, fetchingBook(planNum)),
//calling or dispatching fetchingBook here. it was added in the component using import
fetchingBook__functionalProp:(planNum)=> dispatch(fetchingBook(planNum))
}
}