【发布时间】:2022-01-12 05:22:08
【问题描述】:
我的问题是,当下一个 js 应用程序刷新/重新加载时,redux 存储状态没有更新。我在组件中有以下代码
const Landing = () => {
const freeADS = useSelector((state) => state.ads.freeAds); //this states are working fine without page refresh
useEffect(() => {
dispatch(fetchFreeAds());
}, [])
return(
{freeADS.map((data, i) => {
//some codings.........
})}
)
}
export default Landing;
redux 操作调用
export const fetchFreeAds = () => {
return {
type: ActionTypes.FETCH_FREE_ADS
}
}
rootsaga / watch saga 收到请求后,我调用如下处理程序
export function* handleFreeAds() {
const { response, error } = yield call(fetchFreeAds);
if (response)
{
yield put({type:"SET_FREE_ADS", payload: response.data[0]});
}
else{
}
}
实际的 api 调用在这里
export function fetchFreeAds() {
return axios.get('http://xxxxxxxxxx')
.then(response => ({ response }))
.catch(error => ({ error }))
}
我现在收到此错误。请给予一些支持。谢谢
【问题讨论】:
-
在 API 返回之前不会填充关联的数据。你可以给状态切片一个初始状态。例如。
state = { ads: { freeAds: [] } } -
@slideshowp2 这是管理状态的方法
export interface State{freeAds: null}export const adReducers = (state = {}, {type, payload}) => { ...... }
标签: reactjs redux axios next.js redux-saga