I have a simple useReducer increment counter.
import {useReducer} from 'react'
const counterStateFn = (state,action) => {
if(action.type === "INCREMENT") {
return {count : state.count++}
}
if(action.type === "DECREMENT") {
return {count : state.count--}
}
return {count: 0}
}
const Counter = () => {
const [counterState, modifierCounterState] = useReducer(counterStateFn,{count: 0});
const modifierCounterStateIncrementFn = () => {
modifierCounterState({type:"INCREMENT"});
}
const modifierCounterStateDecrementFn = () => {
modifierCounterState({type:"DECREMENT"})
}
return (
<div className="counter-wrap">
<button onClick={modifierCounterStateIncrementFn}>Increment(+)</button>
<span>Count is: {counterState.count}</span>
<button onClick={modifierCounterStateDecrementFn}>Decrement(-)</button>
</div>
);
}
export default Counter;
Inside counterStateFn
I use {count : state.count++}
and {count : state.count--}
to increment and decrement the counter and this doesn't work.
If I use {count : ++state.count}
or {count : state.count+1}
it will work.
I would like to know why {count : state.count++}
and {count : state.count--}
doesn't work.
Thankyou.