Hacker News new | ask | show | jobs
by ddek 2239 days ago
I can call ‘setTimeout’ from the component?

I honestly don’t see the issue with your example.

I’d use an event handler to work out when something happened. This would set the timeout, and the handle would be stored as a ref. I’d have a use effect hook to clean up the timer, so when the component is unmounted the timeout is cleared.

What’s the issue here?

2 comments

It's no longer declarative as soon as you've done that though; it's still a break in the declarative paradigm.
The issue is it's not as simple as calling `setTimeout` as soon as your use case becomes more complex. See this great post from Dan Abramov about using setInterval with hooks: https://overreacted.io/making-setinterval-declarative-with-r...