【发布时间】:2020-07-17 15:02:51
【问题描述】:
我有 2 个调用不同 API 的操作。我在 useEffect 中调度这些操作。
我有 2 个 reducers 文件,每个文件一个,用于存储从 API 接收到的数据。
所以,基本上我应该能够使用 useState 单独访问这两个数据。
但是第二个调用的 API 的数据是覆盖了第一个 API 的数据。我不明白怎么做,因为它们甚至不在同一个文件上,甚至不相关。
组件
const items = useSelector((state) => state.lostchambers.items);
const lostChambersItems = useSelector((state) => state.sharklostdolsea.itemsLostChamber);
useEffect(() => {
dispatch(fetchingLostChambers());
dispatch(fetchSharkLostDolSea());
}, [dispatch]);
两个文件的操作看起来像这样我只在这里发布一个文件,因为它的代码相同
import { FETCH_POSTS } from "./type";
import axios from "../../utils/Request";
export const fetchingLostChambers = () => async (dispatch) => {
const response = await axios.get("API");
const { data = false, status } = response;
if (status === 200) {
if (data) {
dispatch({
type: FETCH_POSTS,
items: data.acf,
});
}
}
};
这两个操作的 Reducer 看起来像这样,但我只在此处发布一个文件,因为它的代码相同
import { FETCH_POSTS } from "../actions/lostchambers/type";
const initialState = {
items: [],
};
export default (state = initialState, action) => {
switch (action.type) {
case FETCH_POSTS:
return {
...state,
...action,
};
default:
return state;
}
};
组合减速器
import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import rootReducers from "./reducers";
const initialState = {};
const middleware = [thunk];
const store = createStore(rootReducers, initialState, applyMiddleware(...middleware));
export default store;
RootReducer
import { combineReducers } from "redux";
import venues from "./venues";
import lostchambers from "./lostchambers";
import sharklostdolsea from "./sharklostdolsea";
export default combineReducers({
venues,
lostchambers,
sharklostdolsea,
});
我在这里遗漏了什么吗?我就是想不通这个问题,我已经尝试了四个小时了。
【问题讨论】:
-
嘿,我在这里找到了。等待我的回答。不管怎样,你介意给我看看 combineReducers 部分吗?
-
当然,我已将其添加到问题中
-
我需要这个文件里面的部分:
import rootReducers from "./reducers"; -
@CamSong 是的,我也添加了。看来你也遇到过这个问题
标签: reactjs redux react-redux