【发布时间】:2020-10-07 14:38:27
【问题描述】:
我不熟悉使用 redux 和 redux-saga。
当操作发送到 API 时,我无法检索对象的信息。 当我在“promocoesSagas”中第一次使用日志时返回null,它只是在LOAD_REQUEST成功后才与对象一起返回,这很明显。 我应该采取什么方法让我的应用程序等待接收这些数据? 因为如果我调用一个
const promocoesItems = promocoesSagas.data.item;
应用程序因“LOAD_REQUEST”尚未加载数据而中断,第一次返回null。 First time call Trying access the value
我的 useEffect 钩子:
const List = () => {
...
const promocoesSagas = useSelector((state: AppState) => state.promocoes);
console.log(promocoesSagas);
const promocoesItem = promocoesSagas.data.items;
console.log(promocoesItem);
const dispatch = useDispatch<Dispatch<PromocoesActions>>();
useEffect(() => {
(async () => {
setIsloading(true);
try {
dispatch({ type: PromocoesActionTypes.LOAD_REQUEST, payload: query });
} catch (error) {
const errorResult: ErrorResult = error;
if (
errorResult.result != null &&
errorResult.result.statusCode === 403
) {
push('/error-403');
return;
}
message.error(errorResult.message);
}
setIsloading(false);
})();
}, [query, refetch]);
return (...)}
actions.ts
const loadPromocoesAsync = createAsyncAction(
PromocoesActionTypes.LOAD_REQUEST,
PromocoesActionTypes.LOAD_SUCCESS,
PromocoesActionTypes.LOAD_FAILURE,
)<PromocoesQuery, FetchResult<PromocoesModel>, undefined>();
export { loadPromocoesAsync };
减速器
const INITIAL_STATE: PromocoesState = {
data: null,
error: false,
loading: false,
};
const reducer: Reducer<PromocoesState> = (
state = INITIAL_STATE,
action: PromocoesActions,
) => {
switch (action.type) {
case PromocoesActionTypes.LOAD_REQUEST:
return { ...state, loading: true };
case PromocoesActionTypes.LOAD_SUCCESS:
return { ...state, loading: false, error: false, data: action.payload };
case PromocoesActionTypes.LOAD_FAILURE:
return { ...state, loading: false, error: true };
default:
return state;
}
};
sagas.ts
function* load(action: ReturnType<typeof loadPromocoesAsync.request>) {
try {
const query = action.payload;
const response: FetchResult<PromocoesModel> = yield call(
api.getPromocoes,
query,
);
yield put(loadPromocoesAsync.success(response));
} catch (err) {
yield put(loadPromocoesAsync.failure());
}
}
export { load };
types.ts
type PromocoesActions = ActionType<typeof promocoesActions>;
enum PromocoesActionTypes {
LOAD_REQUEST = 'LOAD_REQUEST',
LOAD_SUCCESS = 'LOAD_SUCCESS',
LOAD_FAILURE = 'LOAD_FAILURE',
}
interface PromocoesState {
readonly data: FetchResult<PromocoesModel> | null;
readonly loading: boolean;
readonly error: boolean;
}
export { PromocoesActions, PromocoesActionTypes, PromocoesState };
root-reducer
const rootReducer = combineReducers({
promocoes,
});
export type AppState = ReturnType<typeof rootReducer>;
export default rootReducer;
根传奇
export default function* rootSaga() {
return yield all([takeLatest(loadPromocoesAsync.request, load)]);
}
【问题讨论】:
-
你想在哪里使用
promocoesItem...看来你只是想在console.log的函数组件体中访问它? -
抱歉,如果我无法添加任何必要的信息。在您发表评论后,我看到我的另一个组件得到了错误的返回,并且在我仍在加载的信息传递给另一个组件之前刚刚放置了一个 console.log ()。
-
那么你还在努力解决这个问题,还是已经解决了?
-
已解决,我只是在添加缺少的内容。
标签: reactjs typescript react-redux react-hooks redux-saga