Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lodash debounce in React functional component #140

Open
gracekrcx opened this issue Feb 4, 2023 · 1 comment
Open

lodash debounce in React functional component #140

gracekrcx opened this issue Feb 4, 2023 · 1 comment

Comments

@gracekrcx
Copy link
Owner

lodash debounce in React functional component not working
Lodash debounce not working in React
看起來應該是因為 react functional component render 產生的 instance 不一樣
will be a new function for every render. You can use the useCallback hook to make sure that the same function is being persisted between renders and it will work as expected.

@gracekrcx gracekrcx changed the title lodash debounce in React functional component not working lodash debounce in React functional component Feb 4, 2023
@gracekrcx
Copy link
Owner Author

 // initial render register addEventListener once
    useEffect(() => {
        window.addEventListener('scroll', onScroll)
        return () => window.removeEventListener('scroll', onScroll)
    }, [])

    const onScroll = _.throttle(() => {
        setValue(value)
    }, 100)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant