didact icon indicating copy to clipboard operation
didact copied to clipboard

Add useEffect hook

Open gvergnaud opened this issue 5 years ago • 5 comments

Hi again :)

Here is an attempt at implementing useEffect. It seems to work, but it might be closer to useLayoutEffect since I run the effect right after updating the dom...

Anyway I would be happy to know what you think of this, and how you would have implemented it

gvergnaud avatar Nov 16 '19 14:11 gvergnaud

Seems like the cleanup is never run.

danielpox avatar Mar 10 '21 22:03 danielpox

Hello! The error is in the cancelEffect function, because when the cleanup is added in runEffects, we are updating it to the current version of Fiber, but on the next commit it is converted to the previous version. So, what we must do in cancelEffects is to add in the condition if it has hooks in its old version (fiber.alternate.hooks), then filter the hooks of the old version (fiber.alternate.hooks) and finally execute the cleanups. I hope I've helped ✌

alejooroncoy avatar Nov 07 '21 16:11 alejooroncoy

Hey has anyone used the useState inside this hook? doesnt seem to be working for me

halalsenpai avatar Dec 13 '21 11:12 halalsenpai

Hey has anyone used the useState inside this hook? doesnt seem to be working for me

You need to pass wipFiber to runEffects and cancelEffects instead of fiber runEffects(fiber) -> runEffects(wipFiber)

FrozenIce0617 avatar Feb 20 '22 21:02 FrozenIce0617

@gvergnaud , it's working on my side after replace simply fiber with wipFiber . thank you for your effort.

FrozenIce0617 avatar Feb 20 '22 21:02 FrozenIce0617

This will not work anymore if you use another useState below the useEffect hook, because you will get a mismatch between effect and state hooks in the update procedure of the useState. You will also need to tag the state hooks and filter for the state hooks during the update.

3uler avatar Dec 07 '23 11:12 3uler