[英]How to import variables of state from useSelector?
This is my very simple reducer:这是我非常简单的减速器:
import { createSlice } from "@reduxjs/toolkit";
const initState = {
red: 0,
green: 0,
blue: 0,
};
const colorSlice = createSlice({
name: "color",
initialState: initState,
reducers: {
controlRed(state, action) {
state.red = action.payload;
},
controlGreen(state, action) {
state.green = action.payload;
},
controlBlue(state, action) {
state.blue = action.payload;
},
},
});
export const { controlRed, controlGreen, controlBlue } = colorSlice.actions;
export default colorSlice.reducer;
It works good but how would I go about importing the ( red
, green
, blue
) state variables in my other components?它工作得很好,但是我将如何 go 关于在我的其他组件中导入(
red
、 green
、 blue
) state 变量?
I did this and it doesn't work.我这样做了,但它不起作用。
const { red, green, blue } = useSelector((state) => state.colors);
It gives me this error.它给了我这个错误。
I am new to redux toolkit.我是 redux 工具包的新手。 How should the useSelector look like?
useSelector 应该是什么样子?
EDIT: This is my configureStore:编辑:这是我的 configureStore:
import { configureStore } from "@reduxjs/toolkit";
import reducer from "./color";
const store = configureStore({
reducer: reducer,
});
export default store;
Where should the.colors come from? .colors应该从哪里来?
The color values are not further nested in a "colors" object, they can just be accessed directly, eg颜色值没有进一步嵌套在“颜色”object 中,它们可以直接访问,例如
const { red, green, blue } = useSelector((state) => state);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.