Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
Using redux, why can't I import a json file directly to createSlice?

Why does this work:

import myData from './data.json';

const initialState = myData;

const wevaSlice = createSlice({
    name: 'weva',
    initialState,
    reducers: {
         // whatever
    }
})

and this doesn't ?

import myData from './data.json';

const wevaSlice = createSlice({
    name: 'weva',
    myData,
    reducers: {
         // whatever
    }
})

If I try the 2nd option I got the following error:

Uncaught Error: The slice reducer for key "weva" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.

Any ideas why is this? Does this has anything to do with Immer lib? If yes can anyone explain why the 2nd option doesn't work?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Because these are not positional arguments. initialState there is a shorthand for

const wevaSlice = createSlice({
    name: 'weva',
    initialState: initialState,
    reducers: {
         // whatever
    }
})

So your second snippet is akin to calling

const wevaSlice = createSlice({
    name: 'weva',
    myData: myData,
    reducers: {
         // whatever
    }
})

Instead, you can do

const wevaSlice = createSlice({
    name: 'weva',
    initialState: myData,
    reducers: {
         // whatever
    }
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda