【发布时间】:2021-08-17 19:57:47
【问题描述】:
我正在使用 react-redux 但我看不到我的状态。当我检查我的状态时,它总是返回我的默认值。我检查我的类型,但它返回真值。问题出在哪里 ?感谢您的帮助
我的行动:
import {
SET_MENULIST
} from "../constants/Menu";
export const makeMenu = (menuList) => {
return {
type: SET_MENULIST,
menuList,
};
};
我的减速机:
const menu = (state = {}, action) => {
switch (action.type) {
case SET_MENULIST:
return {
menuList: action.menuList,
};
default:
return "err";
}
};
export default menu;
我的 reducer/index.js(combineReducers):
import { combineReducers } from "redux";
import Auth from "./Auth";
import Theme from "./Theme";
import Menu from "./Menu";
const reducers = combineReducers({
theme: Theme,
auth: Auth,
menu: Menu,
});
export default reducers;
我尝试在我的页面上像 makeMenu(data); 一样使用它并像这样测试它:
const test = useSelector((state) => state.menu);
console.log(test) //Its returns "err" (Default value of my reducer
)
错在哪里?谢谢回复!!!
【问题讨论】:
-
您是如何调度
makeMenu(data)操作的? -
@slideshowp2 我想我没有发送它。也许这就是它不工作的原因。我只是从动作中导入它然后尝试使用它。
-
你能提供一个最小的、可重现的、完整的代码示例吗?
-
SET_MENULIST的类型是什么? -
我从常量导入我的 SET_MENULIST。始终如一:
export const SET_MENULIST = "SET_MENULIST";
标签: reactjs redux react-redux