I am attempting to pass the previous state as an argument to batch state updates in a single cycle using isOpen => index
, but it throws a semantic error TS6133: 'isOpen' is declared but its value is never read.
const initialState = -1;
const [isOpen, setOpen] = useState(initialState);
// Fails
const handleOpen = (index:number) => {
if(index !== isOpen) {
setOpen(isOpen => index);
} else {
setOpen(isOpen => initialState);
}
};
However, this works:
// Passes
const handleOpen = (index:number) => {
if(index !== isOpen) {
setOpen(index);
} else {
setOpen(initialState);
}
};
Just feeling puzzled, anyone curious?