4 ms·
Looks like you've done some homework here. Just a quick note: I followed through to the article on https://fatih-erikli.com/pattern-matching-in-typescript.htm
by zenosmosis 5y ago
Looks like you've done some homework here.
Just a quick note: I followed through to the article on https://fatih-erikli.com/pattern-matching-in-typescript.html https://fatih-erikli.com/pattern-matching-in-typescript.html and noticed the useEffect call doesn't define a dependency array (not even an empty one), which I believe will make the addEventListener add new listeners on every render. Having a removeEventListener on the return of the useEffect would also ensure it gets cleaned up, if pointed to the same event name / handler.
I assume it was just for illustrative purposes, but felt like I should point it out, regardless.
- fatih-erikli 5y agoYes it was for illustrative purposes, thanks for reading the article :)
- fatih-erikli 5y agoTo be honest this is more like a Typescript project and example rather than React. I'm learning Typescript and this is how I try to improve my understanding of Generic types.
- fatih-erikli 5y agoeventEmitter.addEventListener returns a unsubscribe function to remove the event listeners. useEffect hook calls the passed function's return value if it is not a void. https://reactjs.org/docs/hooks-effect.html#example-using-hooks-1 https://reactjs.org/docs/hooks-effect.html#example-using-hoo... So my eventEmitter.addEventListener method in the blog post returns an ubsubscriber when you call it :)