Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - useState hook access state

I have the following states:

let [currentMonth, setCurrentMonth] = useState(new Date().getMonth());
const [checkIn_month, setCheckInMonth] = useState(null);

I have an click event listener assigned directly to JSX's element tbody. Using event delegation to click on the td elements.

And in the following function if i click on a day that is in the previous month i need to decrement the currentMonth state and after that to set the new value of currentMonth in the setCheckInMonth state.

The problem is:

When i use the setCheckInMonth(currentMonth) state hook it gives the old value, not the new one.

let [currentMonth, setCurrentMonth] = useState(new Date().getMonth());
const [checkIn_month, setCheckInMonth] = useState(null);

const selectDate = e => {

 if (e.target.tagName === 'TD') {

   if (e.target.classList.contains('previous-month-day')) {
    setCurrentMonth(currentMonth => currentMonth - 1);
    setCheckInMonth(currentMonth);
   }
  }
}

What if i do something like this:

setCurrentMonth(currentMonth => currentMonth - 1);
setCheckInMonth(currentMonth - 1);

Is this a correct way of doing it ?


1 Answers

setState() is asynchronous. It doesn't immediately mutate(update) the object. So doing this -

setCurrentMonth(currentMonth => currentMonth - 1);

doesn't mean that currentMonth has the updated value that you can use immediately in the next line.

What you can do is -

const newCurrentMonth = currentMonth - 1;
// now use this newCurrentMonth to update the state.
setCurrentMonth(newCurrentMonth );
setCheckInMonth(newCurrentMonth );
like image 130
Arnab Roy Avatar answered Apr 22 '26 18:04

Arnab Roy