Arek Nawo
Sep 6, 2022

--

Hi, unfortunately it's not possible. In React hooks can be defined only at top-level of the component function. Your best approach would be to trigger a state change from the function and useEffect on the outside. Otherwise, if all you need is immediate access to most up-to-date state value, it's also ok to copy the content to a ref on set. Hope this helps.

--

--

Arek Nawo
Arek Nawo

Written by Arek Nawo

Hobbyist. Programmer. Dreamer. JavaScript and TypeScript lover. 👍 World-a-better-place maker. 🌐 https://areknawo.com

Responses (1)