|
| 1 | +import * as React from "react"; |
| 2 | +import { Link, Outlet, useNavigate, useNavigation } from "react-router"; |
| 3 | + |
| 4 | +export default function Transitions() { |
| 5 | + let navigate = useNavigate(); |
| 6 | + let navigation = useNavigation(); |
| 7 | + let [pending, startTransition] = React.useTransition(); |
| 8 | + let [count, setCount] = React.useState(0); |
| 9 | + let [count2, setCount2] = React.useState(0); |
| 10 | + return ( |
| 11 | + <> |
| 12 | + <h1>Transitions</h1> |
| 13 | + <nav> |
| 14 | + Start Over:{" "} |
| 15 | + <a href="/transitions"> |
| 16 | + <code>unstable_transitions=undefined</code> |
| 17 | + </a> |
| 18 | + {" | "} |
| 19 | + <a href="/transitions?transitions=true"> |
| 20 | + <code>unstable_transitions=true</code> |
| 21 | + </a> |
| 22 | + {" | "} |
| 23 | + <a href="/transitions?transitions=false"> |
| 24 | + <code>unstable_transitions=false</code> |
| 25 | + </a> |
| 26 | + </nav> |
| 27 | + <ul style={{ maxWidth: "600px" }}> |
| 28 | + <li> |
| 29 | + <button onClick={() => navigate("/transitions/slow")}> |
| 30 | + Slow navigation with <code>navigate</code> |
| 31 | + </button> |
| 32 | + <ul> |
| 33 | + <li> |
| 34 | + In the current state, <code>useNavigate</code> navigations are not |
| 35 | + wrapped in <code>startTransition</code>, so they don't play nice |
| 36 | + with other transition-aware state updates |
| 37 | + </li> |
| 38 | + <li> |
| 39 | + Fixed by{" "} |
| 40 | + <code> |
| 41 | + <HydrateRouter unstable_transitions={"{"}true{"}"} /> |
| 42 | + </code> |
| 43 | + </li> |
| 44 | + </ul> |
| 45 | + </li> |
| 46 | + |
| 47 | + <li> |
| 48 | + <button |
| 49 | + onClick={() => navigate("/transitions/slow", { flushSync: true })} |
| 50 | + > |
| 51 | + Slow navigation with <code>navigate + flushSync</code> |
| 52 | + </button> |
| 53 | + <ul> |
| 54 | + <li> |
| 55 | + With the new flag, useNavigate automatically wraps the navigation |
| 56 | + in <code>React.startTransition</code>. Passing the{" "} |
| 57 | + <code>flushSync</code> option will opt out of that and apply |
| 58 | + <code>React.flushSync</code> to the underlying state update |
| 59 | + </li> |
| 60 | + </ul> |
| 61 | + </li> |
| 62 | + |
| 63 | + <li> |
| 64 | + <button |
| 65 | + onClick={() => startTransition(() => navigate("/transitions/slow"))} |
| 66 | + > |
| 67 | + Slow navigation with local <code>startTransition + navigate</code> |
| 68 | + </button> |
| 69 | + <ul> |
| 70 | + <li> |
| 71 | + Once you wrap them in <code>startTransition</code>, they play |
| 72 | + nicely with those updates but they prevent our internal |
| 73 | + mid-navigation state updates from surfacing |
| 74 | + </li> |
| 75 | + <li> |
| 76 | + Fixed by{" "} |
| 77 | + <code> |
| 78 | + <HydrateRouter unstable_transitions={"{"}true{"}"} /> |
| 79 | + </code> |
| 80 | + </li> |
| 81 | + </ul> |
| 82 | + </li> |
| 83 | + |
| 84 | + <li> |
| 85 | + <Link to="/transitions/slow">Slow Navigation via <Link></Link> |
| 86 | + <ul> |
| 87 | + <li> |
| 88 | + In the current state, <code><Link></code> navigations are |
| 89 | + not wrapped in startTransition, so they don't play nice with other |
| 90 | + transition-aware state updates |
| 91 | + </li> |
| 92 | + <li> |
| 93 | + Fixed by{" "} |
| 94 | + <code> |
| 95 | + <HydrateRouter unstable_transitions={"{"}true{"}"} /> |
| 96 | + </code> |
| 97 | + </li> |
| 98 | + </ul> |
| 99 | + </li> |
| 100 | + |
| 101 | + <li> |
| 102 | + <Link to="/transitions/parent">/transitions/parent</Link> |
| 103 | + </li> |
| 104 | + <li> |
| 105 | + <Link to="/transitions/parent/child">/transitions/parent/child</Link> |
| 106 | + </li> |
| 107 | + </ul> |
| 108 | + <div> |
| 109 | + <p |
| 110 | + style={{ |
| 111 | + color: pending ? "green" : "black", |
| 112 | + fontWeight: pending ? "bold" : "normal", |
| 113 | + }} |
| 114 | + > |
| 115 | + React Transition: {pending ? "Pending" : "Idle"} |
| 116 | + </p> |
| 117 | + <p |
| 118 | + style={{ |
| 119 | + color: navigation.state !== "idle" ? "green" : "black", |
| 120 | + fontWeight: navigation.state !== "idle" ? "bold" : "normal", |
| 121 | + }} |
| 122 | + > |
| 123 | + React Router Navigation State: {navigation.state} |
| 124 | + </p> |
| 125 | + </div> |
| 126 | + <button onClick={() => setCount((c) => c + 1)}> |
| 127 | + Increment counter w/o transition {count} |
| 128 | + </button>{" "} |
| 129 | + <button |
| 130 | + onClick={() => React.startTransition(() => setCount2((c) => c + 1))} |
| 131 | + > |
| 132 | + Increment counter w/transition {count2} |
| 133 | + </button> |
| 134 | + <Outlet /> |
| 135 | + <p> |
| 136 | + TODO: Is it possible to demonstrate the issue with |
| 137 | + <code>React.useSyncExternalStore</code> where the global opt-out is |
| 138 | + needed? |
| 139 | + </p> |
| 140 | + </> |
| 141 | + ); |
| 142 | +} |
0 commit comments