I’m learning redux-toolkit and I wrote sample code, but when using useDisptach method I get “Cannot read properties of undefined (reading ‘type’)” error. What do you think is the reason for this? I am sharing the code below:
my index.js file:
JavaScript
x
23
23
1
import Head from "next/head";
2
import Nav from "../components/nav/Nav";
3
import { useEffect } from "react";
4
import { useSelector, useDispatch } from "react-redux";
5
import detectDevice from "../redux/device";
6
7
export default function Home() {
8
const dispatch = useDispatch();
9
useEffect(() => {
10
dispatch(detectDevice());
11
}, []);
12
return (
13
<div>
14
<Head>
15
<title>Test</title>
16
<meta name="description" content="Generated by create next app" />
17
<link rel="icon" href="/favicon.ico" />
18
</Head>
19
<Nav />
20
</div>
21
);
22
}
23
my device.js file:
JavaScript
1
16
16
1
import { createSlice } from "@reduxjs/toolkit";
2
export const deviceSlice = createSlice({
3
name: "isMobile",
4
initialState: {
5
value: false,
6
},
7
reducers: {
8
detectDevice: (state) => {
9
state.value = true;
10
},
11
},
12
});
13
export const { detectDevice } = deviceSlice.actions;
14
15
export default deviceSlice.reducer;
16
my store.js file :
JavaScript
1
8
1
import { configureStore } from "@reduxjs/toolkit";
2
import detectDevice from "./device";
3
export default configureStore({
4
reducer: {
5
isMobile: detectDevice,
6
},
7
});
8
Advertisement
Answer
I found where I made a mistake. The import on line 5 in the index.js file should have been like this: import { detectDevice } from “../redux/device”;