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

129
Vistas
Passing array/object in useReducer state but not getting state value

This code is not working giving error 'currentOperand' is not defined

function reducer(state, action) {
  switch(action.type) {
    case 'choose_operation':
      currentOperand = 0;
      return {...state, {/*operation*/}};
  }
}
const [{ currentOperand, previousOperand, operation }, dispatch] = useReducer(reducer, {});
dispatch({type: 'choose_operation'})

Neither this one showing same error 'currentOperand' is not defined

function reducer(state, action) {
  switch(action.type) {
    case 'choose_operation':
      state[0] = 0; // state.currentOperand = 0;
      return {...state, {/*operation*/}};
  }
}
const [[ currentOperand, previousOperand, operation ], dispatch] = useReducer(reducer, []);
dispatch({type: 'choose_operation'})

This one is working but I have to specify the array in every return

function reducer([ currentOperand, previousOperand, operation ], action) {
  switch(action.type) {
    case 'choose_operation':
      currentOperand = 0;
      return [ currentOperand, previousOperand, operation ];
  }
}
const [[ currentOperand, previousOperand, operation ], dispatch] = useReducer(reducer, [",","]);
dispatch({type: 'choose_operation'})
about 4 years ago · Juan Pablo Isaza
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