preact icon indicating copy to clipboard operation
preact copied to clipboard

the child component of ErrorBoundary, wrapped by memo function failed to recover from the last error

Open blinkcat opened this issue 3 years ago • 2 comments

  • [x] Check if updating to the latest Preact version resolves the issue

Describe the bug the child componet wrapped by memo function caused an error in the first render, then it's parent component caught the error and solved the error, but this child component disappears in the next render.

To Reproduce

The second Test component disappeared. https://codesandbox.io/s/charming-curie-to3te?file=/src/index.js

Steps to reproduce the behavior:

Please see the demo above. It's not difficult to reproduce.

Expected behavior

all Test component should be rendered.

blinkcat avatar Feb 13 '22 12:02 blinkcat

This is generally expected when you are using something outside of the closure. When you call memo, much like all the hooks stuff, you are leveraging the variables at that time. You can see that when you pass in causeError as a prop you won't be seeing this issue. https://codesandbox.io/s/headless-cherry-b5z3e?file=/src/index.js:82-582

JoviDeCroock avatar Feb 13 '22 15:02 JoviDeCroock

Hi @JoviDeCroock , thanks for your quick reply. But, I'm still confused about your explanation. I don't think it's related to closures.

  1. the same demo works as I expected in React. https://stackblitz.com/edit/react-efu258?file=src/App.js
  2. the value of causeError variable is always up-to-date in each render.
  3. when you pass in causeError as a prop, it will only make the default shouldComponentUpdate of memorized component return true, then the diff for this component can continue. it is just a kind of workaround.

Am I right?

blinkcat avatar Feb 14 '22 07:02 blinkcat