Skip to content

Add Error Boundary to useTransition API docs #6354

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

Merged
merged 18 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/MDX/Sandpack/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export function Preview({
rawError = null;
}

// When throwing a new Error in Sandpack - we want to disable the dev error dialog
// to show the Error Boundary fallback
if (rawError && rawError.message.includes(`throw Error('Example error')`)) {
rawError = null;
}

// Memoized because it's fed to debouncing.
const firstLintError = useMemo(() => {
if (lintErrors.length === 0) {
Expand Down
111 changes: 103 additions & 8 deletions src/content/reference/react/useTransition.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: useTransition
canary: true
---

<Intro>
Expand Down Expand Up @@ -151,7 +152,7 @@ export default function TabContainer() {

function selectTab(nextTab) {
startTransition(() => {
setTab(nextTab);
setTab(nextTab);
});
}

Expand Down Expand Up @@ -823,7 +824,7 @@ function use(promise) {
reason => {
promise.status = 'rejected';
promise.reason = reason;
},
},
);
throw promise;
}
Expand Down Expand Up @@ -1017,7 +1018,7 @@ function use(promise) {
reason => {
promise.status = 'rejected';
promise.reason = reason;
},
},
);
throw promise;
}
Expand Down Expand Up @@ -1288,7 +1289,7 @@ function use(promise) {
reason => {
promise.status = 'rejected';
promise.reason = reason;
},
},
);
throw promise;
}
Expand Down Expand Up @@ -1332,7 +1333,7 @@ function use(promise) {
reason => {
promise.status = 'rejected';
promise.reason = reason;
},
},
);
throw promise;
}
Expand Down Expand Up @@ -1379,9 +1380,9 @@ async function getBio() {
setTimeout(resolve, 500);
});

return `The Beatles were an English rock band,
formed in Liverpool in 1960, that comprised
John Lennon, Paul McCartney, George Harrison
return `The Beatles were an English rock band,
formed in Liverpool in 1960, that comprised
John Lennon, Paul McCartney, George Harrison
and Ringo Starr.`;
}

Expand Down Expand Up @@ -1501,6 +1502,100 @@ main {

---

### Displaying an error to users with a error boundary {/*displaying-an-error-to-users-with-error-boundary*/}

<Canary>

Error Boundary for useTransition is currently only available in React's canary and experimental channels. Learn more about [React's release channels here](/community/versioning-policy#all-release-channels).

</Canary>

If a function passed to `startTransition` throws an error, you can display an error to your user with an [error boundary](/reference/react/Component#catching-rendering-errors-with-an-error-boundary). To use an error boundary, wrap the component where you are calling the `useTransition` in an error boundary. Once the function passed to `startTransition` errors, the fallback for the error boundary will be displayed.

<Sandpack>

```js AddCommentContainer.js active
import { useTransition } from "react";
import { ErrorBoundary } from "react-error-boundary";

export function AddCommentContainer() {
return (
<ErrorBoundary fallback={<p>⚠️Something went wrong</p>}>
<AddCommentButton />
</ErrorBoundary>
);
}

function addComment(comment) {
// For demonstration purposes to show Error Boundary
if(comment == null){
throw Error('Example error')
}
}

function AddCommentButton() {
const [pending, startTransition] = useTransition();

return (
<button
disabled={pending}
onClick={() => {
startTransition(() => {
// Intentionally not passing a comment
// so error gets thrown
addComment();
});
}}>
Add comment
</button>
);
}
```

```js App.js hidden
import { AddCommentContainer } from "./AddCommentContainer.js";

export default function App() {
return <AddCommentContainer />;
}
```

```js index.js hidden
// TODO: update to import from stable
// react instead of canary once the `use`
// Hook is in a stable release of React
import React, { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './styles.css';

// TODO: update this example to use
// the Codesandbox Server Component
// demo environment once it is created
import App from './App';

const root = createRoot(document.getElementById('root'));
root.render(
<StrictMode>
<App />
</StrictMode>
);
```

```json package.json hidden
{
"dependencies": {
"react": "canary",
"react-dom": "canary",
"react-scripts": "^5.0.0",
"react-error-boundary": "4.0.3"
},
"main": "/index.js"
}
```
</Sandpack>

---

## Troubleshooting {/*troubleshooting*/}

### Updating an input in a transition doesn't work {/*updating-an-input-in-a-transition-doesnt-work*/}
Expand Down
3 changes: 2 additions & 1 deletion src/sidebarReference.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
},
{
"title": "useTransition",
"path": "/reference/react/useTransition"
"path": "/reference/react/useTransition",
"canary": true
}
]
},
Expand Down