Hook traps: UseEffect and Stale Closures

Опубликовано: 24 Февраль 2021
на канале: Zen of Coding
7,047
294

I really like hooks: It has an elegant API that is simple to use, declarative, easy to abstract & Reuse. But still, you can get yourself in a trap in some cases: Stale closures are an example. They happen when your useEffect or other hook is seeing older versions of your props and state, usually when you're doing something asynchronous (like an event or a timer).

In this video, I show three examples of stale closures and how to deal with them properly using:
• The useEffect's dependency array
• useState setter in a functional format
• using a ref instead of a piece of state


Смотрите видео Hook traps: UseEffect and Stale Closures онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Zen of Coding 24 Февраль 2021, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 7,047 раз и оно понравилось 294 людям.