Skip to content

one time effect with useEffect #7

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

Closed
dwhitney opened this issue Jul 4, 2019 · 3 comments
Closed

one time effect with useEffect #7

dwhitney opened this issue Jul 4, 2019 · 3 comments

Comments

@dwhitney
Copy link
Contributor

dwhitney commented Jul 4, 2019

useEffect doesn't appear to work as a "one time effect". I'd like to use it to send a message to a web worker when a component loads, but nothing happens until the component unmounts. I'd use useAff, but because it sets state, I'm getting into a nasty infinite loop in my particular case. If you need an example, I can put one together, but perhaps I just need clarification on how to do a "one time efect"

Thanks!

@maddie927
Copy link
Member

maddie927 commented Jul 4, 2019

The effect will clean up and rerun any time the provided key changes. Try a constant valve as the key, such as unit. Another option is to construct your effect such that rerunning it isn’t a problem. That isn’t always possible though.

@dwhitney
Copy link
Contributor Author

dwhitney commented Jul 8, 2019

ah it does work. It was not clear to me that I was returning the cleanup effect from the function, so everything was wrapped up in that return value. I've made a PR (#8) with some docs clarifying this if anyone else runs into the same issue.

@dwhitney dwhitney closed this as completed Jul 8, 2019
@maddie927
Copy link
Member

Ah yeah, everything could be documented better. I’ve been lazy about things that are in the React docs though 😅

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

2 participants