Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmount hooks should be called during the commit phase to ensure consistency with React #4299

Open
farwayer opened this issue Mar 5, 2024 · 2 comments

Comments

@farwayer
Copy link

farwayer commented Mar 5, 2024

Describe the bug

I've run into issues several times that are caused by calling cleanup functions during the rendering phase. First time in the UI library (a similar case is described here #4222). The second time with a library that prohibits data modification at the rendering stage.

It would be nice for React compatibility and to prevent problems like this to call cleanup functions on hooks after rather than during rendering.

@JoviDeCroock JoviDeCroock added the needs-more-info The issue doesn't contain enough information to be able to help label Mar 11, 2024
@JoviDeCroock
Copy link
Member

JoviDeCroock commented Mar 11, 2024

They are called as part of the commit phase 😅 mind posting a reproduction or something we can base our discussion off of? Or do you mean deferring the hook cleanups? https://github.com/preactjs/preact/blob/main/hooks/src/index.js#L127

@farwayer
Copy link
Author

Yes, you are right. I'm talking about calling the cleanup function of a child component while rendering the parent, if the child component is unmounted. The timing of the call is different from React and this sometimes causes problems.

let Parent = () => {
  console.log('parent render start')
  useLayoutEffect(() => console.log('parent render end'))

  let [showChild, setShowChild] = useState(true)

  useEffect(() => {
    setTimeout(() => setShowChild(false), 1000)
  }, [])

  return showChild ? <Child/> : null
}

let Child = () => {
  useEffect(() => () => console.log('child cleanup'), [])
  return null
}

React:

parent render start
parent render end
parent render start
parent render end
child cleanup

Preact:

parent render start
parent render end
parent render start
child cleanup
parent render end

@JoviDeCroock JoviDeCroock added feature request and removed needs-more-info The issue doesn't contain enough information to be able to help labels Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants