Skip to content

Commit 63f9470

Browse files
kmiddleton14kassenslunaleaps
authored
Add Error Boundary to useTransition API docs (#6354)
* Add Error Boundary to useTransition docs * add Error Boundary to useTransition * Use Canary * Add Error Boundary to useTransition API docs * Update src/components/MDX/Sandpack/Preview.tsx Co-authored-by: Jan Kassens <jan@kassens.net> * Update src/content/reference/react/useTransition.md Co-authored-by: Luna <lunaleaps@gmail.com> * Update src/content/reference/react/useTransition.md Co-authored-by: Luna <lunaleaps@gmail.com> * Add Error Boundary to useTransition docs * add Error Boundary to useTransition * Use Canary * Add Error Boundary to useTransition API docs * Update src/components/MDX/Sandpack/Preview.tsx Co-authored-by: Jan Kassens <jan@kassens.net> * Update src/content/reference/react/useTransition.md Co-authored-by: Luna <lunaleaps@gmail.com> * Update src/content/reference/react/useTransition.md Co-authored-by: Luna <lunaleaps@gmail.com> * Address comments and update usage example * Address comments and update usage example * Update src/content/reference/react/useTransition.md Co-authored-by: Luna <lunaleaps@gmail.com> --------- Co-authored-by: Jan Kassens <jan@kassens.net> Co-authored-by: Luna <lunaleaps@gmail.com>
1 parent bbdbaca commit 63f9470

File tree

3 files changed

+111
-9
lines changed

3 files changed

+111
-9
lines changed

src/components/MDX/Sandpack/Preview.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ export function Preview({
5252
rawError = null;
5353
}
5454

55+
// When throwing a new Error in Sandpack - we want to disable the dev error dialog
56+
// to show the Error Boundary fallback
57+
if (rawError && rawError.message.includes(`throw Error('Example error')`)) {
58+
rawError = null;
59+
}
60+
5561
// Memoized because it's fed to debouncing.
5662
const firstLintError = useMemo(() => {
5763
if (lintErrors.length === 0) {

src/content/reference/react/useTransition.md

Lines changed: 103 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: useTransition
3+
canary: true
34
---
45

56
<Intro>
@@ -151,7 +152,7 @@ export default function TabContainer() {
151152

152153
function selectTab(nextTab) {
153154
startTransition(() => {
154-
setTab(nextTab);
155+
setTab(nextTab);
155156
});
156157
}
157158

@@ -823,7 +824,7 @@ function use(promise) {
823824
reason => {
824825
promise.status = 'rejected';
825826
promise.reason = reason;
826-
},
827+
},
827828
);
828829
throw promise;
829830
}
@@ -1017,7 +1018,7 @@ function use(promise) {
10171018
reason => {
10181019
promise.status = 'rejected';
10191020
promise.reason = reason;
1020-
},
1021+
},
10211022
);
10221023
throw promise;
10231024
}
@@ -1288,7 +1289,7 @@ function use(promise) {
12881289
reason => {
12891290
promise.status = 'rejected';
12901291
promise.reason = reason;
1291-
},
1292+
},
12921293
);
12931294
throw promise;
12941295
}
@@ -1332,7 +1333,7 @@ function use(promise) {
13321333
reason => {
13331334
promise.status = 'rejected';
13341335
promise.reason = reason;
1335-
},
1336+
},
13361337
);
13371338
throw promise;
13381339
}
@@ -1379,9 +1380,9 @@ async function getBio() {
13791380
setTimeout(resolve, 500);
13801381
});
13811382

1382-
return `The Beatles were an English rock band,
1383-
formed in Liverpool in 1960, that comprised
1384-
John Lennon, Paul McCartney, George Harrison
1383+
return `The Beatles were an English rock band,
1384+
formed in Liverpool in 1960, that comprised
1385+
John Lennon, Paul McCartney, George Harrison
13851386
and Ringo Starr.`;
13861387
}
13871388

@@ -1501,6 +1502,100 @@ main {
15011502
15021503
---
15031504
1505+
### Displaying an error to users with a error boundary {/*displaying-an-error-to-users-with-error-boundary*/}
1506+
1507+
<Canary>
1508+
1509+
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).
1510+
1511+
</Canary>
1512+
1513+
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.
1514+
1515+
<Sandpack>
1516+
1517+
```js AddCommentContainer.js active
1518+
import { useTransition } from "react";
1519+
import { ErrorBoundary } from "react-error-boundary";
1520+
1521+
export function AddCommentContainer() {
1522+
return (
1523+
<ErrorBoundary fallback={<p>⚠️Something went wrong</p>}>
1524+
<AddCommentButton />
1525+
</ErrorBoundary>
1526+
);
1527+
}
1528+
1529+
function addComment(comment) {
1530+
// For demonstration purposes to show Error Boundary
1531+
if(comment == null){
1532+
throw Error('Example error')
1533+
}
1534+
}
1535+
1536+
function AddCommentButton() {
1537+
const [pending, startTransition] = useTransition();
1538+
1539+
return (
1540+
<button
1541+
disabled={pending}
1542+
onClick={() => {
1543+
startTransition(() => {
1544+
// Intentionally not passing a comment
1545+
// so error gets thrown
1546+
addComment();
1547+
});
1548+
}}>
1549+
Add comment
1550+
</button>
1551+
);
1552+
}
1553+
```
1554+
1555+
```js App.js hidden
1556+
import { AddCommentContainer } from "./AddCommentContainer.js";
1557+
1558+
export default function App() {
1559+
return <AddCommentContainer />;
1560+
}
1561+
```
1562+
1563+
```js index.js hidden
1564+
// TODO: update to import from stable
1565+
// react instead of canary once the `use`
1566+
// Hook is in a stable release of React
1567+
import React, { StrictMode } from 'react';
1568+
import { createRoot } from 'react-dom/client';
1569+
import './styles.css';
1570+
1571+
// TODO: update this example to use
1572+
// the Codesandbox Server Component
1573+
// demo environment once it is created
1574+
import App from './App';
1575+
1576+
const root = createRoot(document.getElementById('root'));
1577+
root.render(
1578+
<StrictMode>
1579+
<App />
1580+
</StrictMode>
1581+
);
1582+
```
1583+
1584+
```json package.json hidden
1585+
{
1586+
"dependencies": {
1587+
"react": "canary",
1588+
"react-dom": "canary",
1589+
"react-scripts": "^5.0.0",
1590+
"react-error-boundary": "4.0.3"
1591+
},
1592+
"main": "/index.js"
1593+
}
1594+
```
1595+
</Sandpack>
1596+
1597+
---
1598+
15041599
## Troubleshooting {/*troubleshooting*/}
15051600
15061601
### Updating an input in a transition doesn't work {/*updating-an-input-in-a-transition-doesnt-work*/}

src/sidebarReference.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
},
7474
{
7575
"title": "useTransition",
76-
"path": "/reference/react/useTransition"
76+
"path": "/reference/react/useTransition",
77+
"canary": true
7778
}
7879
]
7980
},

0 commit comments

Comments
 (0)