Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

425
Views
next.js @reduxjs/toolkit useSelector devuelve indefinido

js y @reduxjs/toolkit en mi proyecto. Cuando trato de obtener datos de la tienda con el método useSelector, se devuelve un valor indefinido.

aquí mi archivo reductor:

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

aquí mi archivo store.js:

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

y mi archivo 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 dispatchDetectDevice = useDispatch(); let size = useSelector((state) => state.isMobile.value); useEffect(() => { if (window.innerWidth < 576) dispatchDetectDevice(detectDevice(true)); else { dispatchDetectDevice(detectDevice(false)); } console.log(size); }); return ( <div> <Head> <title>Create Next App</title> <meta name="description" content="Generated by create next app" /> <link rel="icon" href="/favicon.ico" /> </Head> <Nav /> </div> ); }

El método console.log() en la línea 15 devuelve un valor indefinido.

¿Cuál cree usted que es la razón de esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No se puede acceder a su estado global a menos que su aplicación haya sido empaquetada con el provider de la Redux store . Esto podría explicar por qué su size no está undefined .

No veo ningún problema en sus archivos store.js ni device.js .

¿Podría adjuntar su archivo _app.js ?

Esto debería estar en él:

 import { Provider } from "react-redux"; import store from "PATH_TO_YOUR_STORE"; function MyApp({ Component, pageProps }) { return ( <Provider store={store}> <Component {...pageProps} /> </Provider> ) } export default MyApp;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!