Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda