Your debounced function is getting created at each re-render, to fix it:
You can use useRef
which returns a ref object which will persist for the full lifetime of the component:
const debounceCheckboxSelection = useRef(
debounce(dispatchCheckbox, 2000);
)
and access its initial value with debounceCheckboxSelection.current
:
<FilterInput
type="checkbox"
name={name}
onChange={() => {
if (checked) {
debounceCheckboxSelection.current('REMOVE_SELECTED_PROPERTY_TYPE', value);
setChecked(false);
return;
}
debounceCheckboxSelection.current('SET_SELECTED_PROPERTY_TYPE', value);
setChecked(true);
}}
checked={checked}
/>
Or you can use useCallback
will returns a memoized version of the callback that only changes when any of its dependencies change:
const debounceCheckboxSelection = useCallback(
() => debounce(dispatchCheckbox, 2000), []
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…