[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"verticals":3,"article-react-use-api-promises-context":44,"search-suggestions":246,"path-membership-react-use-api-promises-context":287,"related-react-use-api-promises-context":288,"code:jsx:true:wvheax":416,"code:jsx:true:1fw9mdd":417,"code:jsx:true:1y56du9":418,"code:jsx:true:1qvk0rf":419,"comments-01a1fce0-de91-712e-bc98-72dba2b10bf5":420},[4,20,32],{"id":5,"slug":6,"name":7,"tagline":8,"description":9,"accentFrom":10,"accentTo":11,"icon":12,"defaultLocale":13,"locales":14,"features":16,"position":19},"019fe637-3d33-714b-b57f-23e163ffca0c","dev","Web Development","Read it. Run it. Prove it.","A post a day on modern web development — most with an editable playground and a quiz that explains every answer. Free, no account needed.","violet-500","cyan-400","◇","en",[13,15],"fa",{"courses":17,"paths":17,"articles":17,"exams":18,"flashcards":18,"packages":17,"community":17,"certificates":17,"teams":17,"commerce":17},true,false,0,{"id":21,"slug":22,"name":23,"tagline":24,"description":25,"accentFrom":26,"accentTo":10,"icon":27,"defaultLocale":13,"locales":28,"features":30,"position":31},"019fe637-3dc2-754c-8657-0f175bfee7c6","lang","Languages","Learn a language the way you learn a codebase.","Grammar explained the way good documentation explains an API — one idea at a time, each with a quiz.","amber-400","⌘",[13,15,29],"es",{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":17,"packages":18,"community":17,"certificates":17,"teams":18,"commerce":18},2,{"id":33,"slug":34,"name":35,"tagline":36,"description":37,"accentFrom":38,"accentTo":39,"icon":40,"defaultLocale":13,"locales":41,"features":42,"position":43},"7b3c16f2-931d-410e-802e-e1fa4edab7de","soft","Soft Skills","The half of the job nobody wrote documentation for.","Weekly, on the parts of working life that decide more than your code does — first weeks, meetings, interviews, promotions, and the people around you. Written from what actually happens, and recorded as a podcast you can listen to on the walk.","emerald-400","teal-300","◉",[13],{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":18,"packages":18,"community":17,"certificates":18,"teams":18,"commerce":18},3,{"id":45,"slug":46,"title":47,"subtitle":48,"excerpt":49,"coverUrl":50,"locale":13,"readingMinutes":51,"publishedAt":52,"viewCount":53,"likeCount":19,"commentCount":19,"author":54,"vertical":59,"topic":60,"tags":63,"_count":74,"playground":76,"body":78,"bodyMd":230,"seo":231,"translationGroupId":234,"series":48,"podcastUrl":48,"verticalId":5,"thread":235,"assessments":237,"translations":242,"quiz":244},"01a1fce0-de91-712e-bc98-72dba2b10bf5","react-use-api-promises-context","`use()` Breaks the Rules of Hooks on Purpose. Here's the Catch.",null,"React's `use()` reads a promise or context mid-render and skips the Rules of Hooks on purpose — but that same freedom hides a caching trap that resuspends your component forever if you miss it.","\u002Fmedia\u002Fcovers\u002Freact-use-api-promises-context.png",6,"2026-11-27T11:49:31.813Z",28,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},"019fe637-3c25-7088-9034-39c9f15dc3c8","Parsa Jiravand","parsa","Frontend engineer · building bestpractic",{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},"react","React",[64,65,68,71],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},"javascript","Javascript",{"slug":69,"name":70,"color":48},"webdev","Webdev",{"slug":72,"name":73,"color":48},"frontend","Frontend",{"assessments":75},1,{"slug":46,"title":77},"use() promise identity — interactive playground",{"blocks":79,"version":75},[80,84,87,92,95,101,104,107,111,114,117,121,124,128,131,134,137,140,143,146,149,152,155,159,162,165,169,172,175,178,181,184,187,190,193,196,199,202,205,212,215,218,221,224],{"id":81,"html":82,"type":83},"b1","\u003Cp>You rip out a \u003Ccode>useEffect\u003C\u002Fcode>, a \u003Ccode>useState\u003C\u002Fcode>, and a cleanup flag, replace all three with one line of \u003Ccode>use()\u003C\u002Fcode>, and the component looks cleaner than it has in months. Then you refresh the page and the loading spinner never stops. Network tab: the same request, firing again, and again, every few milliseconds, forever.\u003C\u002Fp>","paragraph",{"id":85,"html":86,"type":83},"b2","\u003Cp>Nothing in the code is wrong, exactly. You just found the one rule \u003Ccode>use()\u003C\u002Fcode> actually enforces — by being the React API that&#39;s allowed to break every other rule.\u003C\u002Fp>",{"id":88,"html":89,"text":90,"type":91,"level":31},"b3","The dance \u003Ccode>use()\u003C\u002Fcode> was built to end","The dance use() was built to end","heading",{"id":93,"html":94,"type":83},"b4","\u003Cp>Before \u003Ccode>use()\u003C\u002Fcode>, reading an async value inside a component meant writing the same fifteen lines everywhere:\u003C\u002Fp>",{"id":96,"code":97,"type":98,"language":99,"highlight":100},"b5","function Profile({ userId }) {\n  const [user, setUser] = useState(null);\n  const [loading, setLoading] = useState(true);\n\n  useEffect(() => {\n    let cancelled = false;\n    setLoading(true);\n    fetchUser(userId).then((data) => {\n      if (!cancelled) {\n        setUser(data);\n        setLoading(false);\n      }\n    });\n    return () => { cancelled = true; };\n  }, [userId]);\n\n  if (loading) return \u003CSpinner \u002F>;\n  return \u003CUserCard user={user} \u002F>;\n}","code","jsx",[],{"id":102,"html":103,"type":83},"b6","\u003Cp>Two pieces of state tracking one value. A \u003Ccode>cancelled\u003C\u002Fcode> flag so a slow response for the \u003Cem>old\u003C\u002Fem> \u003Ccode>userId\u003C\u002Fcode> doesn&#39;t overwrite a faster one for the new \u003Ccode>userId\u003C\u002Fcode>. None of it is about showing a user card — it&#39;s all plumbing to make \u003Ccode>useEffect\u003C\u002Fcode> behave like something it was never built to be: a way to read an async value.\u003C\u002Fp>",{"id":105,"html":106,"type":83},"b7","\u003Cp>\u003Ccode>use()\u003C\u002Fcode> reads it directly. No \u003Ccode>useEffect\u003C\u002Fcode>, no \u003Ccode>useState\u003C\u002Fcode>, no race-condition flag:\u003C\u002Fp>",{"id":108,"code":109,"type":98,"language":99,"highlight":110},"b8","import { use, Suspense, useMemo } from \"react\";\n\nfunction Profile({ userPromise }) {\n  const user = use(userPromise);\n  return \u003CUserCard user={user} \u002F>;\n}\n\nfunction ProfilePage({ userId }) {\n  const userPromise = useMemo(() => fetchUser(userId), [userId]);\n  return (\n    \u003CSuspense fallback={\u003CSpinner \u002F>}>\n      \u003CProfile userPromise={userPromise} \u002F>\n    \u003C\u002FSuspense>\n  );\n}",[],{"id":112,"html":113,"type":83},"b9","\u003Cp>\u003Ccode>use(userPromise)\u003C\u002Fcode> either returns the resolved value or, if the promise is still pending, \u003Cem>suspends\u003C\u002Fem> — the same mechanism \u003Ccode>React.lazy\u003C\u002Fcode> uses. React walks up to the nearest \u003Ccode>&lt;Suspense&gt;\u003C\u002Fcode> and shows its fallback instead of rendering \u003Ccode>Profile\u003C\u002Fcode> with nothing. When the promise settles, React re-renders \u003Ccode>Profile\u003C\u002Fcode>, and \u003Ccode>use()\u003C\u002Fcode> just returns the value like it was there all along. One line replaced the whole dance, and the race-condition flag is gone because there&#39;s only one state to track: the promise.\u003C\u002Fp>",{"id":115,"html":116,"type":83},"b10","\u003Cp>Before you scroll — does anything in that second version look like it could go wrong? Sit with it for a second.\u003C\u002Fp>",{"id":118,"html":119,"text":120,"type":91,"level":31},"b11","I simplified it one line further. That&#39;s when it broke.","I simplified it one line further. That's when it broke.",{"id":122,"html":123,"type":83},"b12","\u003Cp>The \u003Ccode>useMemo\u003C\u002Fcode> felt like an extra step, so I cut it:\u003C\u002Fp>",{"id":125,"code":126,"type":98,"language":99,"highlight":127},"b13","\u002F\u002F 🚫 looks identical, isn't\nfunction Profile({ userId }) {\n  const user = use(fetchUser(userId));\n  return \u003CUserCard user={user} \u002F>;\n}",[],{"id":129,"html":130,"type":83},"b14","\u003Cp>This is the version that floods the network tab. Here&#39;s why: every time \u003Ccode>Profile\u003C\u002Fcode> renders, \u003Ccode>fetchUser(userId)\u003C\u002Fcode> runs again and hands \u003Ccode>use()\u003C\u002Fcode> a \u003Cem>brand-new\u003C\u002Fem> promise object — even if \u003Ccode>userId\u003C\u002Fcode> hasn&#39;t changed. React has no way to know that this new promise represents &quot;the same request, still loading.&quot; It only knows it was just handed a pending promise it&#39;s never seen before, so it suspends again. The fallback shows, the promise eventually resolves, \u003Ccode>Profile\u003C\u002Fcode> re-renders, and on \u003Cem>that\u003C\u002Fem> render it calls \u003Ccode>fetchUser(userId)\u003C\u002Fcode> one more time — producing yet another new, unseen, pending promise. The loop doesn&#39;t stop because nothing ever breaks the chain.\u003C\u002Fp>",{"id":132,"html":133,"type":83},"b15","\u003Cp>\u003Ccode>useMemo\u003C\u002Fcode> was never decoration. It was the one thing making the promise&#39;s \u003Cem>identity\u003C\u002Fem> stable across renders — the same promise object came back out every time, as long as \u003Ccode>userId\u003C\u002Fcode> stayed the same — so React could recognize &quot;I&#39;ve already seen this one, it&#39;s done, here&#39;s the value&quot; instead of &quot;new pending work, suspend again.&quot;\u003C\u002Fp>",{"id":135,"html":136,"type":83},"b16","\u003Cp>That&#39;s the actual rule \u003Ccode>use()\u003C\u002Fcode> enforces, and it&#39;s stricter than anything \u003Ccode>useEffect\u003C\u002Fcode> ever asked of you: \u003Cstrong>the promise has to come from somewhere that caches it.\u003C\u002Fstrong> That&#39;s usually one of three places — a parent component that memoizes it (like the \u003Ccode>useMemo\u003C\u002Fcode> above), a Server Component, where the promise can be created fresh each request because the request itself is the cache boundary, or a Suspense-integrated data library (React Query, SWR, Relay) that&#39;s already solving this for you. Creating a fresh promise inside the same component that calls \u003Ccode>use()\u003C\u002Fcode> on it, every render, is the one pattern that&#39;s never safe.\u003C\u002Fp>",{"id":138,"html":139,"type":83},"b17","\u003Cp>One thing worth knowing before you lean on the \u003Ccode>useMemo\u003C\u002Fcode> version in production: React&#39;s own docs skip \u003Ccode>useMemo\u003C\u002Fcode> for this and reach straight for a persistent cache (a module-level \u003Ccode>Map\u003C\u002Fcode> keyed by the request, so the same promise comes back no matter what re-renders) — because \u003Ccode>useMemo\u003C\u002Fcode> is documented as a performance optimization, not a guaranteed one. It works in the \u003Ccode>ProfilePage\u003C\u002Fcode>\u002F\u003Ccode>Profile\u003C\u002Fcode> split above specifically because the memo lives in the \u003Cem>parent\u003C\u002Fem>, and the parent never suspends. Move that same \u003Ccode>useMemo\u003C\u002Fcode> down into the component that calls \u003Ccode>use()\u003C\u002Fcode>, and the first suspend throws the memoized value away with it — recreating the exact loop you were trying to fix.\u003C\u002Fp>",{"id":141,"html":142,"type":83},"b18","\u003C!-- playground:start -->",{"id":144,"html":145,"text":145,"type":91,"level":31},"b19","🎮 Try it yourself",{"id":147,"html":148,"type":83},"b20","\u003Cp>\u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Freact-use-api-promises-context\u002Fplayground\">▶️ Open the interactive playground →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":150,"html":151,"type":83},"b21","\u003Cp>\u003Cem>Runs right in your browser — poke at it and watch the concept react live.\u003C\u002Fem>\u003C\u002Fp>",{"id":153,"html":154,"type":83},"b22","\u003C!-- playground:end -->",{"id":156,"html":157,"text":158,"type":91,"level":31},"b23","Why it&#39;s an API, not a hook","Why it's an API, not a hook",{"id":160,"html":161,"type":83},"b24","\u003Cp>Here&#39;s the part that&#39;s easy to miss if you only skim the React docs: \u003Ccode>use()\u003C\u002Fcode> doesn&#39;t sit on the &quot;Hooks&quot; page with \u003Ccode>useState\u003C\u002Fcode> and \u003Ccode>useEffect\u003C\u002Fcode>. It&#39;s listed separately, under React&#39;s general APIs — and that placement isn&#39;t an accident of alphabetizing.\u003C\u002Fp>",{"id":163,"html":164,"type":83},"b25","\u003Cp>Every other hook comes with the Rules of Hooks: call it unconditionally, at the top level, in the same order, every render. Break that rule with \u003Ccode>useState\u003C\u002Fcode> and React&#39;s internal bookkeeping desyncs — state attaches to the wrong slot. \u003Ccode>use()\u003C\u002Fcode> doesn&#39;t have that problem, because it isn&#39;t tied to a slot in the same way. You can call it inside an \u003Ccode>if\u003C\u002Fcode>, after an early return, or inside a loop:\u003C\u002Fp>",{"id":166,"code":167,"type":98,"language":99,"highlight":168},"b26","function Message({ isLoggedIn, messagePromise }) {\n  if (!isLoggedIn) {\n    return \u003CLoginPrompt \u002F>;\n  }\n\n  \u002F\u002F Calling use() AFTER an early return is exactly what useState forbids.\n  const message = use(messagePromise);\n  return \u003Cp>{message}\u003C\u002Fp>;\n}",[],{"id":170,"html":171,"type":83},"b27","\u003Cp>A real hook here would be a lint error. \u003Ccode>use()\u003C\u002Fcode> is fine with it, because React re-evaluates each call to \u003Ccode>use()\u003C\u002Fcode> on its own, by reading from the thing you passed it, not from a fixed position in a call sequence. The same freedom applies to \u003Ccode>use(SomeContext)\u003C\u002Fcode> — unlike \u003Ccode>useContext\u003C\u002Fcode>, you can read it conditionally, which is genuinely useful for a component that only cares about a context value in one branch.\u003C\u002Fp>",{"id":173,"html":174,"type":83},"b28","\u003Cp>The freedom has a boundary, though: you can&#39;t wrap \u003Ccode>use()\u003C\u002Fcode> in your own \u003Ccode>try\u003C\u002Fcode>\u002F\u003Ccode>catch\u003C\u002Fcode>. If the promise rejects, \u003Ccode>use()\u003C\u002Fcode> throws, and React expects an Error Boundary above it to catch that throw — the same contract as a render that throws for any other reason. If you want to handle the rejection yourself instead, attach \u003Ccode>.catch()\u003C\u002Fcode> to the promise \u003Cem>before\u003C\u002Fem> handing it to \u003Ccode>use()\u003C\u002Fcode>, so it never actually rejects on React&#39;s end.\u003C\u002Fp>",{"id":176,"html":177,"text":177,"type":91,"level":31},"b29","The part worth keeping",{"id":179,"html":180,"type":83},"b30","\u003Cp>\u003Ccode>use()\u003C\u002Fcode> isn&#39;t &quot;useEffect but shorter.&quot; It&#39;s a different contract: you hand it something that eventually has a value, and it pauses the render until that value exists — but only if what you hand it stays recognizably the same thing across renders. Skip the caching and you get the one failure mode worse than the \u003Ccode>useEffect\u003C\u002Fcode> dance you were trying to delete: a spinner that looks broken but is actually working exactly as told, forever.\u003C\u002Fp>",{"id":182,"html":183,"type":83},"b31","\u003Cp>Where in your own code is a promise getting created fresh on every render because moving it up one level felt like overkill? That&#39;s the one to check first — before \u003Ccode>use()\u003C\u002Fcode> finds it for you in production.\u003C\u002Fp>",{"id":185,"html":186,"type":83},"b32","\u003C!-- quiz:start -->",{"id":188,"html":189,"text":189,"type":91,"level":31},"b33","🧠 Test yourself",{"id":191,"html":192,"type":83},"b34","\u003Cp>Think it clicked? \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Freact-use-api-promises-context\u002Fquiz\">Take the 6-question quiz →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":194,"html":195,"type":83},"b35","\u003Cp>\u003Cem>Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.\u003C\u002Fem>\u003C\u002Fp>",{"id":197,"html":198,"type":83},"b36","\u003C!-- quiz:end -->",{"id":200,"html":201,"type":83},"b37","\u003C!-- related:start -->",{"id":203,"html":204,"text":204,"type":91,"level":31},"b38","📚 Read next",{"id":206,"type":207,"items":208,"ordered":18},"b39","list",[209,210,211],"\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fcss-style-queries-react-to-custom-properties\">Container queries can check more than size — style queries let components read custom property values\u003C\u002Fa>","\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ferror-cause-rethrow-context\">You&#39;re rethrowing errors and losing context. \u003Ccode>Error.cause\u003C\u002Fcode> fixes that.\u003C\u002Fa>","\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fusesyncexternalstore-tearing-fix\">Your Custom Hooks Tear in React 18. \u003Ccode>useSyncExternalStore\u003C\u002Fcode> Fixes It.\u003C\u002Fa>",{"id":213,"html":214,"type":83},"b40","\u003C!-- related:end -->",{"id":216,"type":217},"b41","divider",{"id":219,"html":220,"type":83},"b42","\u003Cp>🚀 \u003Cstrong>Want more like this?\u003C\u002Fstrong> Every guide, playground, and quiz lives on \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">bestpractic.org\u003C\u002Fa>\u003C\u002Fstrong> — open it and \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">sign up free\u003C\u002Fa>\u003C\u002Fstrong> so the next one finds you.\u003C\u002Fp>",{"id":222,"html":223,"type":83},"b43","\u003Cp>\u003Cem>Thanks for reading! Let&#39;s stay connected:\u003C\u002Fem>\u003C\u002Fp>",{"id":225,"type":207,"items":226,"ordered":18},"b44",[227,228,229],"⭐ \u003Cstrong>GitHub\u003C\u002Fstrong> — follow me and star the projects: \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fparsajiravand\">github.com\u002Fparsajiravand\u003C\u002Fa>","💬 \u003Cstrong>Discord\u003C\u002Fstrong> — join the frontend best-practices community: \u003Ca href=\"https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ\">discord.gg\u002Fd9KRhuAwQ\u003C\u002Fa>","📸 \u003Cstrong>Instagram\u003C\u002Fstrong> — frontend best practices, daily: \u003Ca href=\"https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F\">@bestpractice___\u003C\u002Fa>","You rip out a `useEffect`, a `useState`, and a cleanup flag, replace all three with one line of `use()`, and the component looks cleaner than it has in months. Then you refresh the page and the loading spinner never stops. Network tab: the same request, firing again, and again, every few milliseconds, forever.\n\nNothing in the code is wrong, exactly. You just found the one rule `use()` actually enforces — by being the React API that's allowed to break every other rule.\n\n## The dance `use()` was built to end\n\nBefore `use()`, reading an async value inside a component meant writing the same fifteen lines everywhere:\n\n```jsx\nfunction Profile({ userId }) {\n  const [user, setUser] = useState(null);\n  const [loading, setLoading] = useState(true);\n\n  useEffect(() => {\n    let cancelled = false;\n    setLoading(true);\n    fetchUser(userId).then((data) => {\n      if (!cancelled) {\n        setUser(data);\n        setLoading(false);\n      }\n    });\n    return () => { cancelled = true; };\n  }, [userId]);\n\n  if (loading) return \u003CSpinner \u002F>;\n  return \u003CUserCard user={user} \u002F>;\n}\n```\n\nTwo pieces of state tracking one value. A `cancelled` flag so a slow response for the *old* `userId` doesn't overwrite a faster one for the new `userId`. None of it is about showing a user card — it's all plumbing to make `useEffect` behave like something it was never built to be: a way to read an async value.\n\n`use()` reads it directly. No `useEffect`, no `useState`, no race-condition flag:\n\n```jsx\nimport { use, Suspense, useMemo } from \"react\";\n\nfunction Profile({ userPromise }) {\n  const user = use(userPromise);\n  return \u003CUserCard user={user} \u002F>;\n}\n\nfunction ProfilePage({ userId }) {\n  const userPromise = useMemo(() => fetchUser(userId), [userId]);\n  return (\n    \u003CSuspense fallback={\u003CSpinner \u002F>}>\n      \u003CProfile userPromise={userPromise} \u002F>\n    \u003C\u002FSuspense>\n  );\n}\n```\n\n`use(userPromise)` either returns the resolved value or, if the promise is still pending, *suspends* — the same mechanism `React.lazy` uses. React walks up to the nearest `\u003CSuspense>` and shows its fallback instead of rendering `Profile` with nothing. When the promise settles, React re-renders `Profile`, and `use()` just returns the value like it was there all along. One line replaced the whole dance, and the race-condition flag is gone because there's only one state to track: the promise.\n\nBefore you scroll — does anything in that second version look like it could go wrong? Sit with it for a second.\n\n## I simplified it one line further. That's when it broke.\n\nThe `useMemo` felt like an extra step, so I cut it:\n\n```jsx\n\u002F\u002F 🚫 looks identical, isn't\nfunction Profile({ userId }) {\n  const user = use(fetchUser(userId));\n  return \u003CUserCard user={user} \u002F>;\n}\n```\n\nThis is the version that floods the network tab. Here's why: every time `Profile` renders, `fetchUser(userId)` runs again and hands `use()` a *brand-new* promise object — even if `userId` hasn't changed. React has no way to know that this new promise represents \"the same request, still loading.\" It only knows it was just handed a pending promise it's never seen before, so it suspends again. The fallback shows, the promise eventually resolves, `Profile` re-renders, and on *that* render it calls `fetchUser(userId)` one more time — producing yet another new, unseen, pending promise. The loop doesn't stop because nothing ever breaks the chain.\n\n`useMemo` was never decoration. It was the one thing making the promise's *identity* stable across renders — the same promise object came back out every time, as long as `userId` stayed the same — so React could recognize \"I've already seen this one, it's done, here's the value\" instead of \"new pending work, suspend again.\"\n\nThat's the actual rule `use()` enforces, and it's stricter than anything `useEffect` ever asked of you: **the promise has to come from somewhere that caches it.** That's usually one of three places — a parent component that memoizes it (like the `useMemo` above), a Server Component, where the promise can be created fresh each request because the request itself is the cache boundary, or a Suspense-integrated data library (React Query, SWR, Relay) that's already solving this for you. Creating a fresh promise inside the same component that calls `use()` on it, every render, is the one pattern that's never safe.\n\nOne thing worth knowing before you lean on the `useMemo` version in production: React's own docs skip `useMemo` for this and reach straight for a persistent cache (a module-level `Map` keyed by the request, so the same promise comes back no matter what re-renders) — because `useMemo` is documented as a performance optimization, not a guaranteed one. It works in the `ProfilePage`\u002F`Profile` split above specifically because the memo lives in the *parent*, and the parent never suspends. Move that same `useMemo` down into the component that calls `use()`, and the first suspend throws the memoized value away with it — recreating the exact loop you were trying to fix.\n\n\u003C!-- playground:start -->\n\n## 🎮 Try it yourself\n\n**[▶️ Open the interactive playground →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Freact-use-api-promises-context\u002Fplayground)**\n\n_Runs right in your browser — poke at it and watch the concept react live._\n\n\u003C!-- playground:end -->\n\n## Why it's an API, not a hook\n\nHere's the part that's easy to miss if you only skim the React docs: `use()` doesn't sit on the \"Hooks\" page with `useState` and `useEffect`. It's listed separately, under React's general APIs — and that placement isn't an accident of alphabetizing.\n\nEvery other hook comes with the Rules of Hooks: call it unconditionally, at the top level, in the same order, every render. Break that rule with `useState` and React's internal bookkeeping desyncs — state attaches to the wrong slot. `use()` doesn't have that problem, because it isn't tied to a slot in the same way. You can call it inside an `if`, after an early return, or inside a loop:\n\n```jsx\nfunction Message({ isLoggedIn, messagePromise }) {\n  if (!isLoggedIn) {\n    return \u003CLoginPrompt \u002F>;\n  }\n\n  \u002F\u002F Calling use() AFTER an early return is exactly what useState forbids.\n  const message = use(messagePromise);\n  return \u003Cp>{message}\u003C\u002Fp>;\n}\n```\n\nA real hook here would be a lint error. `use()` is fine with it, because React re-evaluates each call to `use()` on its own, by reading from the thing you passed it, not from a fixed position in a call sequence. The same freedom applies to `use(SomeContext)` — unlike `useContext`, you can read it conditionally, which is genuinely useful for a component that only cares about a context value in one branch.\n\nThe freedom has a boundary, though: you can't wrap `use()` in your own `try`\u002F`catch`. If the promise rejects, `use()` throws, and React expects an Error Boundary above it to catch that throw — the same contract as a render that throws for any other reason. If you want to handle the rejection yourself instead, attach `.catch()` to the promise *before* handing it to `use()`, so it never actually rejects on React's end.\n\n## The part worth keeping\n\n`use()` isn't \"useEffect but shorter.\" It's a different contract: you hand it something that eventually has a value, and it pauses the render until that value exists — but only if what you hand it stays recognizably the same thing across renders. Skip the caching and you get the one failure mode worse than the `useEffect` dance you were trying to delete: a spinner that looks broken but is actually working exactly as told, forever.\n\nWhere in your own code is a promise getting created fresh on every render because moving it up one level felt like overkill? That's the one to check first — before `use()` finds it for you in production.\n\n\u003C!-- quiz:start -->\n\n## 🧠 Test yourself\n\nThink it clicked? **[Take the 6-question quiz →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Freact-use-api-promises-context\u002Fquiz)**\n\n_Instant feedback, a hint on every question, and an explanation for each answer — right or wrong._\n\n\u003C!-- quiz:end -->\n\n\u003C!-- related:start -->\n\n## 📚 Read next\n\n- [Container queries can check more than size — style queries let components read custom property values](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fcss-style-queries-react-to-custom-properties)\n- [You're rethrowing errors and losing context. `Error.cause` fixes that.](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Ferror-cause-rethrow-context)\n- [Your Custom Hooks Tear in React 18. `useSyncExternalStore` Fixes It.](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fusesyncexternalstore-tearing-fix)\n\n\u003C!-- related:end -->\n\n---\n\n🚀 **Want more like this?** Every guide, playground, and quiz lives on **[bestpractic.org](https:\u002F\u002Fbestpractic.org\u002F)** — open it and **[sign up free](https:\u002F\u002Fbestpractic.org\u002F)** so the next one finds you.\n\n*Thanks for reading! Let's stay connected:*\n\n- ⭐ **GitHub** — follow me and star the projects: [github.com\u002Fparsajiravand](https:\u002F\u002Fgithub.com\u002Fparsajiravand)\n- 💬 **Discord** — join the frontend best-practices community: [discord.gg\u002Fd9KRhuAwQ](https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ)\n- 📸 **Instagram** — frontend best practices, daily: [@bestpractice___](https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F)",{"title":47,"canonical":232,"description":233},"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Freact-use-api-promises-context","React's `use()` reads a promise or context mid-render and skips the Rules of Hooks on purpose — but that same freedom hides a caching trap that resuspends your component forever if","01a1fce0-de91-712e-bc98-7454e8586bf2",{"id":236,"locked":18},"01a1fce0-deb4-70b8-939c-4d4382fd91f8",[238],{"id":239,"slug":46,"title":240,"_count":241},"01a1fce0-dec7-70f4-b44b-fb9a2f9669be","Test yourself — React's use() API",{"questions":51},[243],{"locale":13,"slug":46},{"id":239,"slug":46,"title":240,"_count":245,"questionCount":51},{"questions":51},[247,249,251,253,257,261,265,269,273,275,279,283],{"slug":69,"name":70,"articles":248},195,{"slug":66,"name":67,"articles":250},156,{"slug":72,"name":73,"articles":252},103,{"slug":254,"name":255,"articles":256},"tutorial","Tutorial",85,{"slug":258,"name":259,"articles":260},"css","Css",56,{"slug":262,"name":263,"articles":264},"typescript","Typescript",36,{"slug":266,"name":267,"articles":268},"performance","Performance",33,{"slug":270,"name":271,"articles":272},"programming","Programming",26,{"slug":61,"name":62,"articles":274},23,{"slug":276,"name":277,"articles":278},"node","Node",19,{"slug":280,"name":281,"articles":282},"browser","Browser",18,{"slug":284,"name":285,"articles":286},"nextjs","Nextjs",13,[],{"items":289,"meta":413},[290,302,320,338,357,376,395],{"id":45,"slug":46,"title":47,"subtitle":48,"excerpt":49,"coverUrl":50,"locale":13,"readingMinutes":51,"publishedAt":52,"viewCount":291,"likeCount":19,"commentCount":19,"author":292,"vertical":293,"topic":294,"tags":295,"_count":300,"playground":301,"hasQuiz":17,"hasPlayground":17},29,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[296,297,298,299],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":69,"name":70,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":46},{"id":303,"slug":304,"title":305,"subtitle":48,"excerpt":306,"coverUrl":307,"locale":13,"readingMinutes":286,"publishedAt":308,"viewCount":309,"likeCount":19,"commentCount":19,"author":310,"vertical":311,"topic":312,"tags":313,"_count":318,"playground":319,"hasQuiz":17,"hasPlayground":17},"01a1df1b-203c-7390-aa4a-3c6b678e7abe","nextjs-weekly-error-retry-vs-reset","Next.js 16.3 error.js: retry vs reset Aren't Interchangeable","Next.js 16.3 stabilized error.js's retry(), which re-fetches on recovery — reset() only clears state. The real difference, plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnextjs-weekly-error-retry-vs-reset.png","2026-11-27T11:42:24.474Z",10,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":284,"name":285},[314,315,316,317],{"slug":284,"name":285,"color":48},{"slug":61,"name":62,"color":48},{"slug":69,"name":70,"color":48},{"slug":254,"name":255,"color":48},{"assessments":75},{"slug":304},{"id":321,"slug":322,"title":323,"subtitle":48,"excerpt":324,"coverUrl":325,"locale":13,"readingMinutes":51,"publishedAt":326,"viewCount":327,"likeCount":19,"commentCount":19,"author":328,"vertical":329,"topic":330,"tags":331,"_count":336,"playground":337,"hasQuiz":17,"hasPlayground":17},"01a1c5e4-2058-7020-8330-e4bbfc5f5a5c","usedeferredvalue-react-search-lag","You're debouncing search input. `useDeferredValue` skips the timer.","A client-side filter over 10,000 rows doesn't need a server, so there's no network call to debounce — but the keystroke still stutters. useDeferredValue fixes the actual cause: a slow render, not a slow request.","\u002Fmedia\u002Fcovers\u002Fusedeferredvalue-react-search-lag.png","2026-11-27T11:41:22.834Z",9,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[332,333,334,335],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":266,"name":267,"color":48},{"slug":69,"name":70,"color":48},{"assessments":75},{"slug":322},{"id":339,"slug":340,"title":341,"subtitle":48,"excerpt":342,"coverUrl":343,"locale":13,"readingMinutes":344,"publishedAt":345,"viewCount":346,"likeCount":19,"commentCount":19,"author":347,"vertical":348,"topic":349,"tags":350,"_count":355,"playground":356,"hasQuiz":17,"hasPlayground":17},"01a1623b-51e7-7669-89ad-eb54d67696eb","react-weekly-activity-component","React Activity Component: The State Between Render and Remount","React's Activity component hides a subtree without unmounting it — what state, effects, and renders actually do, plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Freact-weekly-activity-component.png",14,"2026-11-02T06:10:18.569Z",425,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[351,352,353,354],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":254,"name":255,"color":48},{"slug":69,"name":70,"color":48},{"assessments":75},{"slug":340},{"id":358,"slug":359,"title":360,"subtitle":48,"excerpt":361,"coverUrl":362,"locale":13,"readingMinutes":363,"publishedAt":364,"viewCount":365,"likeCount":19,"commentCount":19,"author":366,"vertical":367,"topic":368,"tags":369,"_count":374,"playground":375,"hasQuiz":17,"hasPlayground":17},"01a14e81-eaa5-700a-a6f6-3be4ffe47c2b","usesyncexternalstore-tearing-fix","Your Custom Hooks Tear in React 18. `useSyncExternalStore` Fixes It.","A useState + useEffect hook that subscribes to window size, localStorage, or a WebSocket looks fine until React 18's concurrent rendering shows two parts of your UI two different values at once. useSyncExternalStore is the hook built to stop that.","\u002Fmedia\u002Fcovers\u002Fusesyncexternalstore-tearing-fix.png",5,"2026-10-23T06:11:03.306Z",458,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[370,371,372,373],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":69,"name":70,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":359},{"id":377,"slug":378,"title":379,"subtitle":48,"excerpt":380,"coverUrl":381,"locale":13,"readingMinutes":382,"publishedAt":383,"viewCount":384,"likeCount":19,"commentCount":19,"author":385,"vertical":386,"topic":387,"tags":388,"_count":393,"playground":394,"hasQuiz":17,"hasPlayground":17},"01a13e2e-d5ae-707f-a6c5-726f2ed01ab0","react-weekly-useeffectevent-stale-closures","React 19.2 useEffectEvent: Why It Won't Fix Your Deps Array","React's useEffectEvent (stable in 19.2) kills stale closures in Effects without a ref trick — but it's not a deps-array shortcut. Guide + cheat sheet.","\u002Fmedia\u002Fcovers\u002Freact-weekly-useeffectevent-stale-closures.png",15,"2026-10-17T12:20:57.469Z",434,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[389,390,391,392],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":254,"name":255,"color":48},{"slug":69,"name":70,"color":48},{"assessments":75},{"slug":378},{"id":396,"slug":397,"title":398,"subtitle":48,"excerpt":399,"coverUrl":400,"locale":13,"readingMinutes":382,"publishedAt":401,"viewCount":402,"likeCount":19,"commentCount":19,"author":403,"vertical":404,"topic":405,"tags":406,"_count":411,"playground":412,"hasQuiz":17,"hasPlayground":17},"01a120b9-d611-74ef-ad15-a267677c3251","nextjs-weekly-image-priority-quality-changes","Next.js 16 Image: priority Deprecated, quality Now Allowlisted","Next.js 16 quietly deprecated Image's priority for preload and restricts quality to an allowlist (default: 75). What breaks, plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnextjs-weekly-image-priority-quality-changes.png","2026-10-13T06:10:29.815Z",852,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":284,"name":285},[407,408,409,410],{"slug":284,"name":285,"color":48},{"slug":61,"name":62,"color":48},{"slug":69,"name":70,"color":48},{"slug":254,"name":255,"color":48},{"assessments":75},{"slug":397},{"page":75,"perPage":414,"total":274,"totalPages":415},7,4,"\u003Cdiv class=\"shj shj-lang-jsx shj-multiline\" data-lang=\"jsx\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003Cdiv>10\u003C\u002Fdiv>\u003Cdiv>11\u003C\u002Fdiv>\u003Cdiv>12\u003C\u002Fdiv>\u003Cdiv>13\u003C\u002Fdiv>\u003Cdiv>14\u003C\u002Fdiv>\u003Cdiv>15\u003C\u002Fdiv>\u003Cdiv>16\u003C\u002Fdiv>\u003Cdiv>17\u003C\u002Fdiv>\u003Cdiv>18\u003C\u002Fdiv>\u003Cdiv>19\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">function\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Profile\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> userId \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>user\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> setUser\u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-func\">useState\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-num\">null\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>loading\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> setLoading\u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-func\">useState\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bool\">true\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\n  \u003Cspan class=\"shj-func\">useEffect\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n    \u003Cspan class=\"shj-kwd\">let\u003C\u002Fspan> cancelled \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-bool\">false\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-func\">setLoading\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bool\">true\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-func\">fetchUser\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>userId\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">then\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>data\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n      \u003Cspan class=\"shj-kwd\">if\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">!\u003C\u002Fspan>cancelled\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n        \u003Cspan class=\"shj-func\">setUser\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>data\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n        \u003Cspan class=\"shj-func\">setLoading\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bool\">false\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n      \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n    \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> cancelled \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-bool\">true\u003C\u002Fspan>; \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>userId\u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\n  \u003Cspan class=\"shj-kwd\">if\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>loading\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-var\">Spinner\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">\u002F&gt;\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-var\">UserCard\u003C\u002Fspan> \u003Cspan class=\"shj-class\">user\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">={\u003C\u002Fspan>user\u003Cspan class=\"shj-oper\">}\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">\u002F&gt;\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-jsx shj-multiline\" data-lang=\"jsx\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003Cdiv>10\u003C\u002Fdiv>\u003Cdiv>11\u003C\u002Fdiv>\u003Cdiv>12\u003C\u002Fdiv>\u003Cdiv>13\u003C\u002Fdiv>\u003Cdiv>14\u003C\u002Fdiv>\u003Cdiv>15\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">import\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> use\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Suspense\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> useMemo \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">from\u003C\u002Fspan> \u003Cspan class=\"shj-str\">\"react\"\u003C\u002Fspan>;\n\n\u003Cspan class=\"shj-kwd\">function\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Profile\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> userPromise \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> user \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-func\">use\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>userPromise\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-var\">UserCard\u003C\u002Fspan> \u003Cspan class=\"shj-class\">user\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">={\u003C\u002Fspan>user\u003Cspan class=\"shj-oper\">}\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">\u002F&gt;\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\n\u003Cspan class=\"shj-kwd\">function\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ProfilePage\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> userId \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> userPromise \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-func\">useMemo\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">=&gt;\u003C\u002Fspan> \u003Cspan class=\"shj-func\">fetchUser\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>userId\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>userId\u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\n    \u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-var\">Suspense\u003C\u002Fspan> \u003Cspan class=\"shj-class\">fallback\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">={\u003C\u002Fspan>\u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-var\">Spinner\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">\u002F&gt;\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">}\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan>\n      \u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-var\">Profile\u003C\u002Fspan> \u003Cspan class=\"shj-class\">userPromise\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">={\u003C\u002Fspan>userPromise\u003Cspan class=\"shj-oper\">}\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">\u002F&gt;\u003C\u002Fspan>\n    \u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u002F\u003C\u002Fspan>\u003Cspan class=\"shj-var\">Suspense\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan>\n  \u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-jsx shj-multiline\" data-lang=\"jsx\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-cmnt\">\u002F\u002F 🚫 looks identical, isn't\n\u003C\u002Fspan>\u003Cspan class=\"shj-kwd\">function\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Profile\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> userId \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> user \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-func\">use\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-func\">fetchUser\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>userId\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-var\">UserCard\u003C\u002Fspan> \u003Cspan class=\"shj-class\">user\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">={\u003C\u002Fspan>user\u003Cspan class=\"shj-oper\">}\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">\u002F&gt;\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-jsx shj-multiline\" data-lang=\"jsx\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">function\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Message\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> isLoggedIn\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> messagePromise \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">if\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">!\u003C\u002Fspan>isLoggedIn\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n    \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-var\">LoginPrompt\u003C\u002Fspan> \u003Cspan class=\"shj-oper\">\u002F&gt;\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\n  \u003Cspan class=\"shj-cmnt\">\u002F\u002F Calling use() AFTER an early return is exactly what useState forbids.\n\u003C\u002Fspan>  \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> message \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> \u003Cspan class=\"shj-func\">use\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>messagePromise\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u003C\u002Fspan>\u003Cspan class=\"shj-var\">p\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">{\u003C\u002Fspan>message\u003Cspan class=\"shj-oper\">}\u003C\u002Fspan>\u003Cspan class=\"shj-var\">\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&lt;\u002F\u003C\u002Fspan>\u003Cspan class=\"shj-var\">p\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">&gt;\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>",{"locked":18,"total":19,"comments":421},[]]