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'})