【问题标题】:React-toolkit Cannot read properties of undefined (reading 'type') errorReact-toolkit 无法读取未定义的属性(读取“类型”)错误
【发布时间】:2021-11-24 17:00:42
【问题描述】:

我正在学习 redux-toolkit 并编写了示例代码,但是在使用 useDisptach 方法时出现“无法读取未定义的属性(读取'类型')”错误。你觉得是什么原因呢?我正在分享以下代码:

我的 index.js 文件:

import Head from "next/head";
import Nav from "../components/nav/Nav";
import { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import detectDevice from "../redux/device";

export default function Home() {
  const dispatch = useDispatch();
  useEffect(() => {
    dispatch(detectDevice());
  }, []);
  return (
    <div>
      <Head>
        <title>Test</title>
        <meta name="description" content="Generated by create next app" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <Nav />
    </div>
  );
}

我的 device.js 文件:

    import { createSlice } from "@reduxjs/toolkit";
export const deviceSlice = createSlice({
  name: "isMobile",
  initialState: {
    value: false,
  },
  reducers: {
    detectDevice: (state) => {
      state.value = true;
    },
  },
});
export const { detectDevice } = deviceSlice.actions;

export default deviceSlice.reducer;

我的 store.js 文件:

    import { configureStore } from "@reduxjs/toolkit";
import detectDevice from "./device";
export default configureStore({
  reducer: {
    isMobile: detectDevice,
  },
});

【问题讨论】:

    标签: javascript reactjs redux redux-toolkit


    【解决方案1】:

    我找到了我犯错的地方。 index.js 文件中第 5 行的导入应该是这样的: import { detectDevice } from "../redux/device";

    【讨论】:

      猜你喜欢
      • 2022-11-21
      • 2019-04-24
      • 1970-01-01
      • 2018-04-29
      • 2022-12-08
      • 2022-12-01
      • 2022-12-21
      • 2022-12-24
      • 2022-12-25
      相关资源
      最近更新 更多