feat(react)!: Update ErrorBoundary componentStack type#14742
feat(react)!: Update ErrorBoundary componentStack type#14742AbhiPrasad merged 2 commits intodevelopfrom
ErrorBoundary componentStack type#14742Conversation
size-limit report 📦
|
docs/migration/v8-to-v9.md
Outdated
|
|
||
| ### `@sentry/react` | ||
|
|
||
| The `componentStack` field in the `ErrorBoundary` component is now typed as `string | undefined` instead of `string | null | undefined`. This more closely matches the actual behavior of React, which always returns a `string` whenever a component stack is available. `undefined` is only returned if no error has been caught by the error boundary. |
There was a problem hiding this comment.
m: Looking at the code, I don't see componentStack being typed as string | undefined but rather string | null. Should we update the migration note accordingly?
| The `componentStack` field in the `ErrorBoundary` component is now typed as `string | undefined` instead of `string | null | undefined`. This more closely matches the actual behavior of React, which always returns a `string` whenever a component stack is available. `undefined` is only returned if no error has been caught by the error boundary. | |
| The `componentStack` field in the `ErrorBoundary` component is now typed as `string | null` instead of `string | null | undefined`. This more closely matches the actual behavior of React, which always returns a `string` whenever a component stack is available. `undefined` is only returned if no error has been caught by the error boundary. |
i might just also be missing something 😅 - in that case, feel free to disregard!
There was a problem hiding this comment.
Internally we keep the componentStack as null, but to the user it should always be defined. There is actually one scenario where things are now null, I extended the changelog to talk about this.
packages/react/src/errorboundary.tsx
Outdated
| onMount?: (() => void) | undefined; | ||
| /** Called if resetError() is called from the fallback render props function */ | ||
| onReset?: ((error: unknown, componentStack: string | null | undefined, eventId: string | null) => void) | undefined; | ||
| onReset?: ((error: unknown, componentStack: string | null, eventId: string | null) => void) | undefined; |
There was a problem hiding this comment.
Can we remove null here as well?
There was a problem hiding this comment.
Not exactly, I expanded the migration doc to talk about this.
4e80697 to
04f6178
Compare
|
sorry for the delay, refactored this a little more, ready for another round! |
resolves #14310
supercedes #14327 (I have left @kunal-511 a shout out on the changelog for getting this work started!)
The principle thing that drove this change was this todo:
sentry-javascript/packages/react/src/errorboundary.tsx
Lines 101 to 102 in 5b77377
Specifically we wanted to remove
nullas a valid state fromcomponentStack, making sure that all exposed public API see it asstring | undefined. React always returns astringcomponentStackfrom the error boundary, so this matches our typings more closely to react.By making this change, we can also clean up the
renderlogic a little. Specifically we can check forstate.componentStack === nullto determine if a fallback is rendered, and then I went ahead and removed some unnecessary nesting.