Skip to content

Commit 792f12c

Browse files
vutrantresko
authored andcommitted
fix(hooks): memoize some returned methods in useDatepicker
1 parent ad313fc commit 792f12c

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

packages/hooks/src/useDatepicker/useDatepicker.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useState, useEffect} from 'react'
1+
import {useState, useEffect, useCallback} from 'react'
22
import isBefore from 'date-fns/isBefore'
33
import isAfter from 'date-fns/isAfter'
44
import addDays from 'date-fns/addDays'
@@ -289,44 +289,53 @@ export function useDatepicker({
289289
}
290290
}
291291

292-
function goToPreviousMonths() {
292+
const goToPreviousMonths = useCallback(() => {
293293
setActiveMonths(getNextActiveMonth(activeMonths, numberOfMonths, -1))
294294
setFocusedDate(null)
295-
}
295+
}, [activeMonths, numberOfMonths])
296296

297-
function goToPreviousMonthsByOneMonth() {
297+
const goToPreviousMonthsByOneMonth = useCallback(() => {
298298
setActiveMonths(getNextActiveMonth(activeMonths, numberOfMonths, -1, 1))
299299
setFocusedDate(null)
300-
}
300+
}, [activeMonths, numberOfMonths])
301301

302-
function goToNextMonths() {
302+
const goToNextMonths = useCallback(() => {
303303
setActiveMonths(getNextActiveMonth(activeMonths, numberOfMonths, 1))
304304
setFocusedDate(null)
305-
}
305+
}, [activeMonths, numberOfMonths])
306306

307-
function goToNextMonthsByOneMonth() {
307+
const goToNextMonthsByOneMonth = useCallback(() => {
308308
setActiveMonths(getNextActiveMonth(activeMonths, numberOfMonths, 1, 1))
309309
setFocusedDate(null)
310-
}
310+
}, [activeMonths, numberOfMonths])
311311

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+
)
316319

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+
)
323329

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+
)
330339

331340
return {
332341
firstDayOfWeek,

0 commit comments

Comments
 (0)