It looks like the useEffectOnce doesn’t have the b...
# react
g
It looks like the useEffectOnce doesn’t have the behavior I’m expecting. Either I misunderstood it or it’s not working properly : Whenever I update a component above the one using this useEffectOnce component AND there is a dom update to be made => Then the useEffectOnce is triggered again. Is it the expected behavior ? I’ve instead implemented my own hook for such a purpose with useRef, any opinion ? Example in the answers :
Output after spamming the button (trigger state change) in the parent of the component using the hook
t
Looks like you need
key
for child 🙂
Copy code
onceEffectComponent {
    key = "unique-key"
}
child
isn’t required
cc @Sergei Grishchenko
g
Wow thanks! 🙂 It did work for my example. Now I’m trying to put it a bit deeper inside other components and the same happens ? 🤔 Btw the same happens for my useRef it gets reseted (the identity doesn’t seem to work in nested components somehow?)
Calls for this “onceComponent” at level 1 and level 2
The components simple code :
t
key
for parent?
1
s
I think it is expected behavior because every time you click button you mount/unmount component, so it's life-cycle starts from the beginning, that is why effect hooks behave like that.
Once
here means once per mount/unmount life-cycle. Hack with
key
looks strange for me, I will check how does it works with original JS React
Here example with
key
and it behaves like your first example if I am not mistaken https://codesandbox.io/s/cranky-shannon-tk23j?file=/src/App.tsx
p
@Sergei Grishchenko I think your example is not quite exactly the same as the one provided by @GUIGAL Allan in kotlin. To me, it looks like it should be this: https://codesandbox.io/s/kind-sea-45wqs?file=/src/App.tsx. NB: It works like the second example (i.e. with key in kotlin) with or without key which seems to me like the correct behavior.
👍 1
s
oh, thank you, now I've got it
👍 2
g
I can confirm the second example from Paul, we don’t observe the same behavior with our project and debugging in Kotlin JS. Thankfully the key hack gives us a solution for now.
Do you have any idea where it could be coming from ? Should I open an issue on GitHub ?
t
Do you mean
kotlin-wrappers
issue?
g
I think so ?
t
Yes, looks like issue for
kotlin-wrappers
1
With thread link in description
👍 1