Skip to content

Add documentation note that useEffect comparison is shallow #27

@kevindice

Description

@kevindice

A use-case one might attempt is:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions