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

124
Views
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
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!