[英]Im getting invalid hook call error with useCallBack in React
我在我的項目中遇到無效的鈎子調用錯誤。
<DateBottomSheet
key={keyIndexStartDate}
currentDate={startDate}
headerTitle={moment(startDate).format('MMMM YYYY')}
backdropComponent={renderBackdrop}
ref={sheetRefStartDate}
snapPoints={snapPointsDateSheet}
markedDates={markedStartDate}
onChange={() => handleSheetChanges}
renderArrow={renderCalendarRightAndLeftIcon}
onPressArrowRight={()=>onPressCalendarRight(startDate, 'startDate')}
onPressArrowLeft={()=>onPressCalendarLeft(startDate, 'startDate')}
onDayPress={day => {
setMarkedStartDateValue(day.dateString);
pressOpenStartTimerAction();
// handleSnapPress(1, CreateWorkyBS.startTime);
}}
onPressCancel={() =>
handleCloseStartCalender(CreateWorkyBS.startDate)
}
onPressSave={() => saveStartDateItem()}
saveButtonDisable={buttonStartDateDisable}></DateBottomSheet>
並且是我的 OnpressCalendar function
const onPressCalendarRight = (date, type) =>
useCallback(add => {
const addDate = moment(date).add(1, 'M');
type == 'startDate' ? setStartDate(addDate) : setEndDate(addDate);
add();
});
有人幫我嗎?
你違反了鈎子規則。 useCallback
是一個鈎子,因此只能在功能組件的根級別調用。
看起來您可以簡單地完全刪除useCallback
的使用來解決問題,並直接在onPressCalendarRight
。
const onPressCalendarRight = (date, type) => {
const addDate = moment(date).add(1, 'M');
type == 'startDate' ? setStartDate(addDate) : setEndDate(addDate);
}
嘗試這個
onPressCalendarRight = useCallback((add) => (date,type) => {
const addDate = moment(date).add(1, 'M');
type == 'startDate' ? setStartDate(addDate) : setEndDate(addDate);
add();
},[]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.