|
1 | | -import {useState, useEffect} from 'react' |
| 1 | +import {useState, useEffect, useCallback} from 'react' |
2 | 2 | import isBefore from 'date-fns/isBefore' |
3 | 3 | import isAfter from 'date-fns/isAfter' |
4 | 4 | import addDays from 'date-fns/addDays' |
@@ -289,44 +289,53 @@ export function useDatepicker({ |
289 | 289 | } |
290 | 290 | } |
291 | 291 |
|
292 | | - function goToPreviousMonths() { |
| 292 | + const goToPreviousMonths = useCallback(() => { |
293 | 293 | setActiveMonths(getNextActiveMonth(activeMonths, numberOfMonths, -1)) |
294 | 294 | setFocusedDate(null) |
295 | | - } |
| 295 | + }, [activeMonths, numberOfMonths]) |
296 | 296 |
|
297 | | - function goToPreviousMonthsByOneMonth() { |
| 297 | + const goToPreviousMonthsByOneMonth = useCallback(() => { |
298 | 298 | setActiveMonths(getNextActiveMonth(activeMonths, numberOfMonths, -1, 1)) |
299 | 299 | setFocusedDate(null) |
300 | | - } |
| 300 | + }, [activeMonths, numberOfMonths]) |
301 | 301 |
|
302 | | - function goToNextMonths() { |
| 302 | + const goToNextMonths = useCallback(() => { |
303 | 303 | setActiveMonths(getNextActiveMonth(activeMonths, numberOfMonths, 1)) |
304 | 304 | setFocusedDate(null) |
305 | | - } |
| 305 | + }, [activeMonths, numberOfMonths]) |
306 | 306 |
|
307 | | - function goToNextMonthsByOneMonth() { |
| 307 | + const goToNextMonthsByOneMonth = useCallback(() => { |
308 | 308 | setActiveMonths(getNextActiveMonth(activeMonths, numberOfMonths, 1, 1)) |
309 | 309 | setFocusedDate(null) |
310 | | - } |
| 310 | + }, [activeMonths, numberOfMonths]) |
311 | 311 |
|
312 | | - function goToDate(date: Date) { |
313 | | - setActiveMonths(getInitialMonths(numberOfMonths, date)) |
314 | | - setFocusedDate(null) |
315 | | - } |
| 312 | + const goToDate = useCallback( |
| 313 | + (date: Date) => { |
| 314 | + setActiveMonths(getInitialMonths(numberOfMonths, date)) |
| 315 | + setFocusedDate(null) |
| 316 | + }, |
| 317 | + [numberOfMonths], |
| 318 | + ) |
316 | 319 |
|
317 | | - function goToPreviousYear(numYears: number = 1) { |
318 | | - setActiveMonths( |
319 | | - getNextActiveMonth(activeMonths, numberOfMonths, -(numYears * 12 - numberOfMonths + 1)), |
320 | | - ) |
321 | | - setFocusedDate(null) |
322 | | - } |
| 320 | + const goToPreviousYear = useCallback( |
| 321 | + (numYears: number = 1) => { |
| 322 | + setActiveMonths( |
| 323 | + getNextActiveMonth(activeMonths, numberOfMonths, -(numYears * 12 - numberOfMonths + 1)), |
| 324 | + ) |
| 325 | + setFocusedDate(null) |
| 326 | + }, |
| 327 | + [activeMonths, numberOfMonths], |
| 328 | + ) |
323 | 329 |
|
324 | | - function goToNextYear(numYears: number = 1) { |
325 | | - setActiveMonths( |
326 | | - getNextActiveMonth(activeMonths, numberOfMonths, numYears * 12 - numberOfMonths + 1), |
327 | | - ) |
328 | | - setFocusedDate(null) |
329 | | - } |
| 330 | + const goToNextYear = useCallback( |
| 331 | + (numYears: number = 1) => { |
| 332 | + setActiveMonths( |
| 333 | + getNextActiveMonth(activeMonths, numberOfMonths, numYears * 12 - numberOfMonths + 1), |
| 334 | + ) |
| 335 | + setFocusedDate(null) |
| 336 | + }, |
| 337 | + [activeMonths, numberOfMonths], |
| 338 | + ) |
330 | 339 |
|
331 | 340 | return { |
332 | 341 | firstDayOfWeek, |
|
0 commit comments