Redux, 状态未定义

huangapple go评论85阅读模式
英文:

Redux, state not defined

问题

以下是代码的翻译部分:

Console error:
控制台错误:
Uncaught TypeError: state.options is undefined
未捕获的类型错误:state.options 未定义

Provider setup:
提供程序设置:

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { store } from "./Utilities/store";
import { Provider } from "react-redux";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<>



</>
);

英文:

trying to make an option to change the color of text,border,background but i get the error state is not defined in the console.it says that state.options.text that i addressed in option page is not defined,
i checked the routing and it is Ok

option page

import React from &#39;react&#39;
import { useSelector, useDispatch } from &#39;react-redux&#39;
import {textCH, borderCh, backgroundCh} from &#39;../Utilities/options/theme&#39;
function Options() {

    const textcolor = useSelector((state)=&gt;state.options.text)

    console.log(textcolor);
    return (
        &lt;div style={{ paddingLeft: &#39;20px&#39; }}&gt;
            &lt;h1&gt;OPTIONS&lt;/h1&gt;

            text color: &lt;input type={&#39;color&#39;} /&gt; 
            border color: &lt;input type={&#39;color&#39;} /&gt; 
            background color: &lt;input type={&#39;color&#39;} /&gt;
        &lt;/div&gt;
    )
}

export default Options

Reducer

import { createSlice } from &#39;@reduxjs/toolkit&#39;

const initialState = {
   text:&#39;#000000&#39;,
    border:&#39;#000000&#39;,
    background:&#39;#FFFFFF&#39;,
}

export const optionsSlice = createSlice({
  name: &#39;options&#39;,
  initialState,
  reducers: {
    textCH: (state,action) =&gt; {
         state.text = action.payload
    },
    borderCh: (state,action) =&gt; {
      state.border = action.payload
    },
    backgroundCh: (state, action) =&gt; {
      state.background = action.payload
    },
  },
})
export const { textCH, borderCh, backgroundCh } = optionsSlice.actions
export default optionsSlice.reducer


Store file


import { configureStore } from &#39;@reduxjs/toolkit&#39;
import {optionsSlice} from &#39;./options/theme&#39;
export const store = configureStore({
  reducer: {
    options: optionsSlice,
  },
})

Console error:

Uncaught TypeError: state.options is undefined
textcolor Options.jsx:6

Provider setup:

     import React from &quot;react&quot;;
        import ReactDOM from &quot;react-dom/client&quot;;
        import App from &quot;./App&quot;;
        import {store} from &quot;./Utilities/store&quot;;
        import { Provider } from &quot;react-redux&quot;;
        
        const root = ReactDOM.createRoot(document.getElementById(&quot;root&quot;));
        root.render(
          &lt;&gt;
            &lt;Provider store={store}&gt;
              &lt;App /&gt;
            &lt;/Provider&gt;
          &lt;/&gt;
        );

答案1

得分: 1

你在 store.js 中导入了切片本身,而不是 reducer。

不要调用命名的函数:

import { optionsSlice } from './options/theme';

我们应该调用默认的:

import optionsSlice from './options/theme';
英文:

You're importing the slice itself in the store.js, not reducer.

instead of calling the named function:

import {optionsSlice} from &#39;./options/theme&#39;

we should call the default one:

import optionsSlice from &#39;./options/theme&#39;

huangapple
  • 本文由 发表于 2023年2月16日 18:57:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定