News for hackers. Stuff that matters.
Re:useStateMachine: A ½ kb state machine hook for React by jakelazaroff on Saturday May 22, 2021 @10:19AM (#27246764)
Note that you shouldn’t (can’t?) actually pass an async function directly to useEffect, since it returns a promise: https://github.com/facebook/react/issues/14326
Re:useStateMachine: A ½ kb state machine hook for React by jacobedawson on Saturday May 22, 2021 @10:48AM (#27247033)
Not true, see Gaeron's direct reply in the thread you linked to, that's been the standard for 3 years and works like a charm - useEffect specifically is the hook for handling async functions ("impure effects").
Re:useStateMachine: A ½ kb state machine hook for React by azangru on Saturday May 22, 2021 @11:01AM (#27247168)
Notice the words "pass to useEffect" in the parent comment, and the code snippet in the grandparent they are commenting on. The type of the callback function that useEffect accepts is () => void rather than () => Promise<unknown>. What's not true about that?
Re:useStateMachine: A ½ kb state machine hook for React by jakelazaroff on Saturday May 22, 2021 @11:06AM (#27247214)
I reworded it to make it a bit more clear (at least, I thought so) — "pass an async function to useEffect", meaning the argument supplied to useEffect itself, rather than "use directly within" which is ambiguous. Re-added "directly" to hopefully make even more clear. Sorry for the confusion!
Re:useStateMachine: A ½ kb state machine hook for React by zebnyc on Saturday May 22, 2021 @02:30PM (#27249079)
I usually use an IIFE to encapsulate the async block inside the useEffect. Works like a charm!
Re:useStateMachine: A ½ kb state machine hook for React by tanaypingalkar on Saturday May 22, 2021 @04:32PM (#27250014)
True, you cannot do that in useEffect, it will throw you error. And probably your linter will tell you. You can't even use your own or third party hooks in side useEffect. This also throw error I don't know why?
Re:useStateMachine: A ½ kb state machine hook for React by gassiss on Saturday May 22, 2021 @11:45PM (#27252262)
Because you could be nesting hooks, which would bring all sorts of weird behavior.
Re:useStateMachine: A ½ kb state machine hook for React by mrighele on Saturday May 22, 2021 @01:24PM (#27248531)
you're right, good catch. Usually it's the TS compiler that prevents me doing that.