如果我的措辭不正確,請原諒我。我現(xiàn)在正在學習 React 并且在將參數(shù)傳遞給動作時遇到問題。這是操作:export const getPosts = filterQs => { return dispatch => { const apiUrl = wpApi.listPosts; if (filterQs) { apiUrl += "?" + filterQs; } // Actually get the posts dispatch(getPostsStarted()); axios.get(`${apiUrl}`) .then(response => { dispatch(getPostsSuccess(response.data)); }) .catch(error => { dispatch(getPostsFailure(error.message)); }); }}包含if (filterQs)的條件被跳過,因為在調度操作時它似乎實際上沒有該參數(shù)。它在調度時仍然返回一個成功的響應,它只是不包括我需要的參數(shù)。這是我在組件中映射調度的位置:const mapDispatchToProps = { filterUpdate, getPosts}這是實際調用它的方法。changeHandler (event) { const checkedStatus = event.currentTarget.checked; const selectedTaxonomy = event.currentTarget.name; const termId = event.currentTarget.value; const paramsToSend = { checkedStatus: checkedStatus, selectedTaxonomy: selectedTaxonomy, termId: termId }; const filterUpdatePromise = new Promise((resolve, reject) => { this.props.filterUpdate(paramsToSend); resolve(true); }); // Run the filter query string format function only after the props have updated filterUpdatePromise.then(() => { const filterQs = this.formatNewFilterQuery(); getPosts(filterQs); }); }據(jù)我所知,正在執(zhí)行 getPosts(filterQs) 。如果我在 action 的 dispatch 之外記錄 filterQs,我實際上會在日志中看到我的參數(shù)值。我只是不明白為什么我不能將參數(shù)傳遞到調度中。
React:我不能將參數(shù)傳遞給動作嗎?
回首憶惘然
2021-12-23 19:39:33