|
1 | 1 | --- |
2 | 2 | title: useTransition |
| 3 | +canary: true |
3 | 4 | --- |
4 | 5 |
|
5 | 6 | <Intro> |
@@ -151,7 +152,7 @@ export default function TabContainer() { |
151 | 152 |
|
152 | 153 | function selectTab(nextTab) { |
153 | 154 | startTransition(() => { |
154 | | - setTab(nextTab); |
| 155 | + setTab(nextTab); |
155 | 156 | }); |
156 | 157 | } |
157 | 158 |
|
@@ -823,7 +824,7 @@ function use(promise) { |
823 | 824 | reason => { |
824 | 825 | promise.status = 'rejected'; |
825 | 826 | promise.reason = reason; |
826 | | - }, |
| 827 | + }, |
827 | 828 | ); |
828 | 829 | throw promise; |
829 | 830 | } |
@@ -1017,7 +1018,7 @@ function use(promise) { |
1017 | 1018 | reason => { |
1018 | 1019 | promise.status = 'rejected'; |
1019 | 1020 | promise.reason = reason; |
1020 | | - }, |
| 1021 | + }, |
1021 | 1022 | ); |
1022 | 1023 | throw promise; |
1023 | 1024 | } |
@@ -1288,7 +1289,7 @@ function use(promise) { |
1288 | 1289 | reason => { |
1289 | 1290 | promise.status = 'rejected'; |
1290 | 1291 | promise.reason = reason; |
1291 | | - }, |
| 1292 | + }, |
1292 | 1293 | ); |
1293 | 1294 | throw promise; |
1294 | 1295 | } |
@@ -1332,7 +1333,7 @@ function use(promise) { |
1332 | 1333 | reason => { |
1333 | 1334 | promise.status = 'rejected'; |
1334 | 1335 | promise.reason = reason; |
1335 | | - }, |
| 1336 | + }, |
1336 | 1337 | ); |
1337 | 1338 | throw promise; |
1338 | 1339 | } |
@@ -1379,9 +1380,9 @@ async function getBio() { |
1379 | 1380 | setTimeout(resolve, 500); |
1380 | 1381 | }); |
1381 | 1382 |
|
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 |
1385 | 1386 | and Ringo Starr.`; |
1386 | 1387 | } |
1387 | 1388 |
|
@@ -1501,6 +1502,100 @@ main { |
1501 | 1502 |
|
1502 | 1503 | --- |
1503 | 1504 |
|
| 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 | +
|
1504 | 1599 | ## Troubleshooting {/*troubleshooting*/} |
1505 | 1600 |
|
1506 | 1601 | ### Updating an input in a transition doesn't work {/*updating-an-input-in-a-transition-doesnt-work*/} |
|
0 commit comments