You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const [debouncedAddress] = useDebounce({
address1,
address2,
country,
city,
state,
zip,
}, 1500)
useEffect(() => {
// do API call to update address on server
}, [
debouncedAddress.address1
debouncedAddress.address2
debouncedAddress.country,
debouncedAddress.city,
debouncedAddress.state,
debouncedAddress.zip,
])
...such that the form is auto-saved if the user has not typed in the last 1.5 seconds.
But in JS {} !== {} is true.
Suggesting the user use a hash function would be best. Otherwise, the above code example will trigger a re-render every 1500 ms. This should be accompanied w/ a note about the exhaustive-deps eslint rule.
The text was updated successfully, but these errors were encountered:
For your case, you can use useDebounceCallback, which is more configurable:
const[debouncedAddress,setDeboucedAddress]=useState({
address1,
address2,
country,
city,
state,
zip,});const[callback]=useDebounceCallback((newAddress)=>{if(!deepEqual(newAddress,debouncedAddress)){setDeboucedAddress(newAddress);}},1500);useEffect(()=>{// do API call to update address on server},[debouncedAddress.address1debouncedAddress.address2debouncedAddress.country,debouncedAddress.city,debouncedAddress.state,debouncedAddress.zip,])
A use-case one might attempt is:
...such that the form is auto-saved if the user has not typed in the last 1.5 seconds.
But in JS
{} !== {}
istrue
.Suggesting the user use a hash function would be best. Otherwise, the above code example will trigger a re-render every 1500 ms. This should be accompanied w/ a note about the exhaustive-deps eslint rule.
The text was updated successfully, but these errors were encountered: