From e565ef3392d8a5eed5528b08b6be8f184c59e1db Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Wed, 17 Sep 2025 14:09:25 -0400 Subject: [PATCH] fix compiler errors I went through the list of existing errors and tried to separate the expected errors from those that seem to be flagging unexpected issues. In particular, our effects validations are flagging patterns that our own docs examples use. I added todos for these and will follow up later. --- src/content/learn/describing-the-ui.md | 2 +- src/content/learn/escape-hatches.md | 2 +- src/content/learn/keeping-components-pure.md | 10 +++++----- .../learn/lifecycle-of-reactive-effects.md | 4 ++-- .../learn/preserving-and-resetting-state.md | 2 +- .../learn/react-compiler/introduction.md | 2 +- .../learn/referencing-values-with-refs.md | 6 +++--- .../learn/removing-effect-dependencies.md | 4 ++-- src/content/learn/responding-to-events.md | 2 +- .../learn/separating-events-from-effects.md | 6 +++--- src/content/learn/state-a-components-memory.md | 6 +++--- .../learn/synchronizing-with-effects.md | 9 +++++---- src/content/learn/updating-objects-in-state.md | 10 +++++----- .../learn/you-might-not-need-an-effect.md | 16 ++++++++-------- .../reference/react-dom/client/hydrateRoot.md | 3 ++- .../reference/react-dom/createPortal.md | 3 ++- src/content/reference/react/cache.md | 4 ++-- src/content/reference/react/useCallback.md | 12 ++++++------ .../reference/react/useDeferredValue.md | 6 +++--- src/content/reference/react/useEffect.md | 7 +++++-- src/content/reference/react/useLayoutEffect.md | 16 ++++++++++------ src/content/reference/react/useMemo.md | 13 +++++++------ src/content/reference/react/useRef.md | 2 +- src/content/reference/react/useTransition.md | 18 +++++++++--------- .../rules/components-and-hooks-must-be-pure.md | 8 ++++---- .../rules/react-calls-components-and-hooks.md | 6 +++--- 26 files changed, 95 insertions(+), 84 deletions(-) diff --git a/src/content/learn/describing-the-ui.md b/src/content/learn/describing-the-ui.md index 34ee0c01a..339f04223 100644 --- a/src/content/learn/describing-the-ui.md +++ b/src/content/learn/describing-the-ui.md @@ -474,7 +474,7 @@ By strictly only writing your components as pure functions, you can avoid an ent -```js +```js {expectedErrors: {'react-compiler': [5]}} let guest = 0; function Cup() { diff --git a/src/content/learn/escape-hatches.md b/src/content/learn/escape-hatches.md index 23f11f54e..0b2d595b2 100644 --- a/src/content/learn/escape-hatches.md +++ b/src/content/learn/escape-hatches.md @@ -201,7 +201,7 @@ There are two common cases in which you don't need Effects: For example, you don't need an Effect to adjust some state based on other state: -```js {5-9} +```js {expectedErrors: {'react-compiler': [8]}} {5-9} function Form() { const [firstName, setFirstName] = useState('Taylor'); const [lastName, setLastName] = useState('Swift'); diff --git a/src/content/learn/keeping-components-pure.md b/src/content/learn/keeping-components-pure.md index 70049e58e..fafd488e3 100644 --- a/src/content/learn/keeping-components-pure.md +++ b/src/content/learn/keeping-components-pure.md @@ -93,7 +93,7 @@ Here is a component that breaks this rule: -```js +```js {expectedErrors: {'react-compiler': [5]}} let guest = 0; function Cup() { @@ -380,7 +380,7 @@ The buggy code is in `Profile.js`. Make sure you read it all from top to bottom! -```js src/Profile.js +```js {expectedErrors: {'react-compiler': [7]}} src/Profile.js import Panel from './Panel.js'; import { getImageUrl } from './utils.js'; @@ -602,7 +602,7 @@ export default function StoryTray({ stories }) { } ``` -```js src/App.js hidden +```js {expectedErrors: {'react-compiler': [16]}} src/App.js hidden import { useState, useEffect } from 'react'; import StoryTray from './StoryTray.js'; @@ -698,7 +698,7 @@ export default function StoryTray({ stories }) { } ``` -```js src/App.js hidden +```js {expectedErrors: {'react-compiler': [16]}} src/App.js hidden import { useState, useEffect } from 'react'; import StoryTray from './StoryTray.js'; @@ -790,7 +790,7 @@ export default function StoryTray({ stories }) { } ``` -```js src/App.js hidden +```js {expectedErrors: {'react-compiler': [16]}} src/App.js hidden import { useState, useEffect } from 'react'; import StoryTray from './StoryTray.js'; diff --git a/src/content/learn/lifecycle-of-reactive-effects.md b/src/content/learn/lifecycle-of-reactive-effects.md index 3dc9a75f0..72a2e7755 100644 --- a/src/content/learn/lifecycle-of-reactive-effects.md +++ b/src/content/learn/lifecycle-of-reactive-effects.md @@ -1131,7 +1131,7 @@ If you see a linter rule being suppressed, remove the suppression! That's where -```js +```js {expectedErrors: {'react-compiler': [16]}} import { useState, useEffect } from 'react'; export default function App() { @@ -1374,7 +1374,7 @@ export default function App() { } ``` -```js src/ChatRoom.js active +```js {expectedErrors: {'react-compiler': [8]}} src/ChatRoom.js active import { useState, useEffect } from 'react'; export default function ChatRoom({ roomId, createConnection }) { diff --git a/src/content/learn/preserving-and-resetting-state.md b/src/content/learn/preserving-and-resetting-state.md index 11d398d23..041fae355 100644 --- a/src/content/learn/preserving-and-resetting-state.md +++ b/src/content/learn/preserving-and-resetting-state.md @@ -704,7 +704,7 @@ Here, the `MyTextField` component function is defined *inside* `MyComponent`: -```js +```js {expectedErrors: {'react-compiler': [7]}} import { useState } from 'react'; export default function MyComponent() { diff --git a/src/content/learn/react-compiler/introduction.md b/src/content/learn/react-compiler/introduction.md index 9957b8227..96fa8802a 100644 --- a/src/content/learn/react-compiler/introduction.md +++ b/src/content/learn/react-compiler/introduction.md @@ -28,7 +28,7 @@ React Compiler automatically optimizes your React application at build time. Rea Without the compiler, you need to manually memoize components and values to optimize re-renders: -```js +```js {expectedErrors: {'react-compiler': [4]}} import { useMemo, useCallback, memo } from 'react'; const ExpensiveComponent = memo(function ExpensiveComponent({ data, onClick }) { diff --git a/src/content/learn/referencing-values-with-refs.md b/src/content/learn/referencing-values-with-refs.md index fab6599f2..4386e2bdc 100644 --- a/src/content/learn/referencing-values-with-refs.md +++ b/src/content/learn/referencing-values-with-refs.md @@ -211,7 +211,7 @@ If you tried to implement this with a ref, React would never re-render the compo -```js +```js {expectedErrors: {'react-compiler': [13]}} import { useRef } from 'react'; export default function Counter() { @@ -313,7 +313,7 @@ Regular variables like `let timeoutID` don't "survive" between re-renders becaus -```js +```js {expectedErrors: {'react-compiler': [10]}} import { useState } from 'react'; export default function Chat() { @@ -418,7 +418,7 @@ This button is supposed to toggle between showing "On" and "Off". However, it al -```js +```js {expectedErrors: {'react-compiler': [10]}} import { useRef } from 'react'; export default function Toggle() { diff --git a/src/content/learn/removing-effect-dependencies.md b/src/content/learn/removing-effect-dependencies.md index 9a848862a..7ab6dbc1f 100644 --- a/src/content/learn/removing-effect-dependencies.md +++ b/src/content/learn/removing-effect-dependencies.md @@ -303,7 +303,7 @@ Suppressing the linter leads to very unintuitive bugs that are hard to find and -```js +```js {expectedErrors: {'react-compiler': [14]}} import { useState, useEffect } from 'react'; export default function Timer() { @@ -794,7 +794,7 @@ It is important to declare it as a dependency! This ensures, for example, that i -```js +```js {expectedErrors: {'react-compiler': [10]}} import { useState, useEffect } from 'react'; import { createConnection } from './chat.js'; diff --git a/src/content/learn/responding-to-events.md b/src/content/learn/responding-to-events.md index 17bd087ed..78474217c 100644 --- a/src/content/learn/responding-to-events.md +++ b/src/content/learn/responding-to-events.md @@ -546,7 +546,7 @@ Clicking this button is supposed to switch the page background between white and -```js +```js {expectedErrors: {'react-compiler': [5, 7]}} export default function LightSwitch() { function handleClick() { let bodyStyle = document.body.style; diff --git a/src/content/learn/separating-events-from-effects.md b/src/content/learn/separating-events-from-effects.md index 03223183b..fd603c395 100644 --- a/src/content/learn/separating-events-from-effects.md +++ b/src/content/learn/separating-events-from-effects.md @@ -711,7 +711,7 @@ Here, `url` inside `onVisit` corresponds to the *latest* `url` (which could have In the existing codebases, you may sometimes see the lint rule suppressed like this: -```js {7-9} +```js {expectedErrors: {'react-compiler': [8]}} {7-9} function Page({ url }) { const { items } = useContext(ShoppingCartContext); const numberOfItems = items.length; @@ -735,7 +735,7 @@ Can you see why? -```js +```js {expectedErrors: {'react-compiler': [16]}} import { useState, useEffect } from 'react'; export default function App() { @@ -990,7 +990,7 @@ To fix this code, it's enough to follow the rules. ``` -```js +```js {expectedErrors: {'react-compiler': [14]}} import { useState, useEffect } from 'react'; export default function Timer() { diff --git a/src/content/learn/state-a-components-memory.md b/src/content/learn/state-a-components-memory.md index 73d46bdab..0efe1191d 100644 --- a/src/content/learn/state-a-components-memory.md +++ b/src/content/learn/state-a-components-memory.md @@ -23,7 +23,7 @@ Here's a component that renders a sculpture image. Clicking the "Next" button sh -```js +```js {expectedErrors: {'react-compiler': [7]}} import { sculptureList } from './data.js'; export default function Gallery() { @@ -1229,7 +1229,7 @@ When you type into the input fields, nothing appears. It's like the input values -```js +```js {expectedErrors: {'react-compiler': [6]}} export default function Form() { let firstName = ''; let lastName = ''; @@ -1337,7 +1337,7 @@ Are there any limitations on _where_ Hooks may be called? Does this component br -```js +```js {expectedErrors: {'react-compiler': [9]}} import { useState } from 'react'; export default function FeedbackForm() { diff --git a/src/content/learn/synchronizing-with-effects.md b/src/content/learn/synchronizing-with-effects.md index c0ad34709..b5400314a 100644 --- a/src/content/learn/synchronizing-with-effects.md +++ b/src/content/learn/synchronizing-with-effects.md @@ -95,7 +95,7 @@ You might be tempted to try to call `play()` or `pause()` during rendering, but -```js +```js {expectedErrors: {'react-compiler': [7, 9]}} import { useState, useRef, useEffect } from 'react'; function VideoPlayer({ src, isPlaying }) { @@ -1468,7 +1468,8 @@ This component shows the biography for the selected person. It loads the biograp -```js src/App.js +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [9]}} src/App.js import { useState, useEffect } from 'react'; import { fetchBio } from './api.js'; @@ -1541,7 +1542,8 @@ To fix this race condition, add a cleanup function: -```js src/App.js +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [9]}} src/App.js import { useState, useEffect } from 'react'; import { fetchBio } from './api.js'; @@ -1605,4 +1607,3 @@ In addition to ignoring the result of an outdated API call, you can also use [`A - diff --git a/src/content/learn/updating-objects-in-state.md b/src/content/learn/updating-objects-in-state.md index 93ea93bd2..ca6585145 100644 --- a/src/content/learn/updating-objects-in-state.md +++ b/src/content/learn/updating-objects-in-state.md @@ -55,7 +55,7 @@ This example holds an object in state to represent the current pointer position. -```js +```js {expectedErrors: {'react-compiler': [11]}} import { useState } from 'react'; export default function MovingDot() { @@ -209,7 +209,7 @@ These input fields don't work because the `onChange` handlers mutate the state: -```js +```js {expectedErrors: {'react-compiler': [11, 15, 19]}} import { useState } from 'react'; export default function Form() { @@ -832,7 +832,7 @@ Your task is to fix all of these bugs. As you fix them, explain why each of them -```js +```js {expectedErrors: {'react-compiler': [11]}} import { useState } from 'react'; export default function Scoreboard() { @@ -988,7 +988,7 @@ If something unexpected changes, there is a mutation. Find the mutation in `App. -```js src/App.js +```js {expectedErrors: {'react-compiler': [17]}} src/App.js import { useState } from 'react'; import Background from './Background.js'; import Box from './Box.js'; @@ -1293,7 +1293,7 @@ This is the same buggy example as in the previous challenge. This time, fix the -```js src/App.js +```js {expectedErrors: {'react-compiler': [18]}} src/App.js import { useState } from 'react'; import { useImmer } from 'use-immer'; import Background from './Background.js'; diff --git a/src/content/learn/you-might-not-need-an-effect.md b/src/content/learn/you-might-not-need-an-effect.md index 88e490e1a..39a1dc740 100644 --- a/src/content/learn/you-might-not-need-an-effect.md +++ b/src/content/learn/you-might-not-need-an-effect.md @@ -34,7 +34,7 @@ To help you gain the right intuition, let's look at some common concrete example Suppose you have a component with two state variables: `firstName` and `lastName`. You want to calculate a `fullName` from them by concatenating them. Moreover, you'd like `fullName` to update whenever `firstName` or `lastName` change. Your first instinct might be to add a `fullName` state variable and update it in an Effect: -```js {5-9} +```js {expectedErrors: {'react-compiler': [8]}} {5-9} function Form() { const [firstName, setFirstName] = useState('Taylor'); const [lastName, setLastName] = useState('Swift'); @@ -66,7 +66,7 @@ function Form() { This component computes `visibleTodos` by taking the `todos` it receives by props and filtering them according to the `filter` prop. You might feel tempted to store the result in state and update it from an Effect: -```js {4-8} +```js {expectedErrors: {'react-compiler': [7]}} {4-8} function TodoList({ todos, filter }) { const [newTodo, setNewTodo] = useState(''); @@ -165,7 +165,7 @@ Also note that measuring performance in development will not give you the most a This `ProfilePage` component receives a `userId` prop. The page contains a comment input, and you use a `comment` state variable to hold its value. One day, you notice a problem: when you navigate from one profile to another, the `comment` state does not get reset. As a result, it's easy to accidentally post a comment on a wrong user's profile. To fix the issue, you want to clear out the `comment` state variable whenever the `userId` changes: -```js {4-7} +```js {expectedErrors: {'react-compiler': [6]}} {4-7} export default function ProfilePage({ userId }) { const [comment, setComment] = useState(''); @@ -208,7 +208,7 @@ Sometimes, you might want to reset or adjust a part of the state on a prop chang This `List` component receives a list of `items` as a prop, and maintains the selected item in the `selection` state variable. You want to reset the `selection` to `null` whenever the `items` prop receives a different array: -```js {5-8} +```js {expectedErrors: {'react-compiler': [7]}} {5-8} function List({ items }) { const [isReverse, setIsReverse] = useState(false); const [selection, setSelection] = useState(null); @@ -819,7 +819,7 @@ Simplify this component by removing all the unnecessary state and Effects. -```js +```js {expectedErrors: {'react-compiler': [12, 16, 20]}} import { useState, useEffect } from 'react'; import { initialTodos, createTodo } from './todos.js'; @@ -1022,7 +1022,7 @@ One solution is to add a `useMemo` call to cache the visible todos. There is als -```js +```js {expectedErrors: {'react-compiler': [11]}} import { useState, useEffect } from 'react'; import { initialTodos, createTodo, getVisibleTodos } from './todos.js'; @@ -1106,7 +1106,7 @@ Remove the state variable and the Effect, and instead add a `useMemo` call to ca -```js +```js {expectedErrors: {'react-compiler': [8]}} import { useState, useMemo } from 'react'; import { initialTodos, createTodo, getVisibleTodos } from './todos.js'; @@ -1363,7 +1363,7 @@ export default function ContactList({ } ``` -```js src/EditContact.js active +```js {expectedErrors: {'react-compiler': [8, 9]}} src/EditContact.js active import { useState, useEffect } from 'react'; export default function EditContact({ savedContact, onSave }) { diff --git a/src/content/reference/react-dom/client/hydrateRoot.md b/src/content/reference/react-dom/client/hydrateRoot.md index b74e2c38e..b297e54a2 100644 --- a/src/content/reference/react-dom/client/hydrateRoot.md +++ b/src/content/reference/react-dom/client/hydrateRoot.md @@ -296,7 +296,8 @@ import App from './App.js'; hydrateRoot(document.getElementById('root'), ); ``` -```js src/App.js active +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [7]}} src/App.js active import { useState, useEffect } from "react"; export default function App() { diff --git a/src/content/reference/react-dom/createPortal.md b/src/content/reference/react-dom/createPortal.md index e806660e8..e9bd5a819 100644 --- a/src/content/reference/react-dom/createPortal.md +++ b/src/content/reference/react-dom/createPortal.md @@ -398,7 +398,8 @@ Here is a complete example you can play with: } ``` -```js src/App.js +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [15]}} src/App.js import { useRef, useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import { createMapWidget, addPopupToMapWidget } from './map-widget.js'; diff --git a/src/content/reference/react/cache.md b/src/content/reference/react/cache.md index 442584808..ac8950249 100644 --- a/src/content/reference/react/cache.md +++ b/src/content/reference/react/cache.md @@ -323,7 +323,8 @@ All mentioned APIs offer memoization but the difference is what they're intended In general, you should use [`useMemo`](/reference/react/useMemo) for caching an expensive computation in a Client Component across renders. As an example, to memoize a transformation of data within a component. -```jsx {4} +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```jsx {expectedErrors: {'react-compiler': [4]}} {4} 'use client'; function WeatherReport({record}) { @@ -495,4 +496,3 @@ function App() { ); } ``` - diff --git a/src/content/reference/react/useCallback.md b/src/content/reference/react/useCallback.md index acae804ec..9a1520f44 100644 --- a/src/content/reference/react/useCallback.md +++ b/src/content/reference/react/useCallback.md @@ -130,7 +130,7 @@ function ProductPage({ productId, referrer, theme }) { orderDetails, }); } - + return (
{/* ... so ShippingForm's props will never be the same, and it will re-render every time */} @@ -207,7 +207,7 @@ The difference is in *what* they're letting you cache: If you're already familiar with [`useMemo`,](/reference/react/useMemo) you might find it helpful to think of `useCallback` as this: -```js +```js {expectedErrors: {'react-compiler': [3]}} // Simplified implementation (inside React) function useCallback(fn, dependencies) { return useMemo(() => fn, dependencies); @@ -222,7 +222,7 @@ function useCallback(fn, dependencies) { #### Should you add useCallback everywhere? {/*should-you-add-usecallback-everywhere*/} -If your app is like this site, and most interactions are coarse (like replacing a page or an entire section), memoization is usually unnecessary. On the other hand, if your app is more like a drawing editor, and most interactions are granular (like moving shapes), then you might find memoization very helpful. +If your app is like this site, and most interactions are coarse (like replacing a page or an entire section), memoization is usually unnecessary. On the other hand, if your app is more like a drawing editor, and most interactions are granular (like moving shapes), then you might find memoization very helpful. Caching a function with `useCallback` is only valuable in a few cases: @@ -310,7 +310,7 @@ function post(url, data) { } ``` -```js src/ShippingForm.js +```js {expectedErrors: {'react-compiler': [7, 8]}} src/ShippingForm.js import { memo, useState } from 'react'; const ShippingForm = memo(function ShippingForm({ onSubmit }) { @@ -449,7 +449,7 @@ function post(url, data) { } ``` -```js src/ShippingForm.js +```js {expectedErrors: {'react-compiler': [7, 8]}} src/ShippingForm.js import { memo, useState } from 'react'; const ShippingForm = memo(function ShippingForm({ onSubmit }) { @@ -868,7 +868,7 @@ When you find which dependency is breaking memoization, either find a way to rem Suppose the `Chart` component is wrapped in [`memo`](/reference/react/memo). You want to skip re-rendering every `Chart` in the list when the `ReportList` component re-renders. However, you can't call `useCallback` in a loop: -```js {5-14} +```js {expectedErrors: {'react-compiler': [6]}} {5-14} function ReportList({ items }) { return (
diff --git a/src/content/reference/react/useDeferredValue.md b/src/content/reference/react/useDeferredValue.md index ed561c977..bf716b173 100644 --- a/src/content/reference/react/useDeferredValue.md +++ b/src/content/reference/react/useDeferredValue.md @@ -244,7 +244,7 @@ input { margin: 10px; } -A common alternative UI pattern is to *defer* updating the list of results and to keep showing the previous results until the new results are ready. Call `useDeferredValue` to pass a deferred version of the query down: +A common alternative UI pattern is to *defer* updating the list of results and to keep showing the previous results until the new results are ready. Call `useDeferredValue` to pass a deferred version of the query down: ```js {3,11} export default function App() { @@ -669,7 +669,7 @@ export default function App() { } ``` -```js src/SlowList.js +```js {expectedErrors: {'react-compiler': [19, 20]}} src/SlowList.js import { memo } from 'react'; const SlowList = memo(function SlowList({ text }) { @@ -746,7 +746,7 @@ export default function App() { } ``` -```js src/SlowList.js +```js {expectedErrors: {'react-compiler': [19, 20]}} src/SlowList.js import { memo } from 'react'; const SlowList = memo(function SlowList({ text }) { diff --git a/src/content/reference/react/useEffect.md b/src/content/reference/react/useEffect.md index e93e61102..a250bb15a 100644 --- a/src/content/reference/react/useEffect.md +++ b/src/content/reference/react/useEffect.md @@ -928,7 +928,8 @@ Note the `ignore` variable which is initialized to `false`, and is set to `true` -```js src/App.js +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [9]}} src/App.js import { useState, useEffect } from 'react'; import { fetchBio } from './api.js'; @@ -1737,7 +1738,9 @@ If your app uses server rendering (either [directly](/reference/react-dom/server In rare cases, you might need to display different content on the client. For example, if your app reads some data from [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage), it can't possibly do that on the server. Here is how you could implement this: -```js + +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [5]}} function MyComponent() { const [didMount, setDidMount] = useState(false); diff --git a/src/content/reference/react/useLayoutEffect.md b/src/content/reference/react/useLayoutEffect.md index d38458f14..7598d1a28 100644 --- a/src/content/reference/react/useLayoutEffect.md +++ b/src/content/reference/react/useLayoutEffect.md @@ -48,7 +48,7 @@ function Tooltip() { #### Parameters {/*parameters*/} * `setup`: The function with your Effect's logic. Your setup function may also optionally return a *cleanup* function. Before your component is added to the DOM, React will run your setup function. After every re-render with changed dependencies, React will first run the cleanup function (if you provided it) with the old values, and then run your setup function with the new values. Before your component is removed from the DOM, React will run your cleanup function. - + * **optional** `dependencies`: The list of all reactive values referenced inside of the `setup` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. If you omit this argument, your Effect will re-run after every re-render of the component. #### Returns {/*returns*/} @@ -87,7 +87,8 @@ To do this, you need to render in two passes: **All of this needs to happen before the browser repaints the screen.** You don't want the user to see the tooltip moving. Call `useLayoutEffect` to perform the layout measurements before the browser repaints the screen: -```js {5-8} +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [7]}} {5-8} function Tooltip() { const ref = useRef(null); const [tooltipHeight, setTooltipHeight] = useState(0); // You don't know real height yet @@ -187,7 +188,8 @@ export default function ButtonWithTooltip({ tooltipContent, ...rest }) { } ``` -```js src/Tooltip.js active +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [11]}} src/Tooltip.js active import { useRef, useLayoutEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import TooltipContainer from './TooltipContainer.js'; @@ -337,7 +339,8 @@ export default function ButtonWithTooltip({ tooltipContent, ...rest }) { } ``` -```js src/Tooltip.js active +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [11]}} src/Tooltip.js active import { useRef, useLayoutEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import TooltipContainer from './TooltipContainer.js'; @@ -484,7 +487,8 @@ export default function ButtonWithTooltip({ tooltipContent, ...rest }) { } ``` -```js src/Tooltip.js active +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [11]}} src/Tooltip.js active import { useRef, useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import TooltipContainer from './TooltipContainer.js'; @@ -627,7 +631,7 @@ export default function ButtonWithTooltip({ tooltipContent, ...rest }) { } ``` -```js src/Tooltip.js active +```js {expectedErrors: {'react-compiler': [10, 11]}} src/Tooltip.js active import { useRef, useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import TooltipContainer from './TooltipContainer.js'; diff --git a/src/content/reference/react/useMemo.md b/src/content/reference/react/useMemo.md index f0bdd77fb..bbb2ad622 100644 --- a/src/content/reference/react/useMemo.md +++ b/src/content/reference/react/useMemo.md @@ -149,7 +149,7 @@ Also note that measuring performance in development will not give you the most a #### Should you add useMemo everywhere? {/*should-you-add-usememo-everywhere*/} -If your app is like this site, and most interactions are coarse (like replacing a page or an entire section), memoization is usually unnecessary. On the other hand, if your app is more like a drawing editor, and most interactions are granular (like moving shapes), then you might find memoization very helpful. +If your app is like this site, and most interactions are coarse (like replacing a page or an entire section), memoization is usually unnecessary. On the other hand, if your app is more like a drawing editor, and most interactions are granular (like moving shapes), then you might find memoization very helpful. Optimizing with `useMemo` is only valuable in a few cases: @@ -225,7 +225,8 @@ export default function App() { ``` -```js src/TodoList.js active +{/* TODO(@poteto) - investigate potential false positives in react compiler validation */} +```js {expectedErrors: {'react-compiler': [5]}} src/TodoList.js active import { useMemo } from 'react'; import { filterTodos } from './utils.js' @@ -717,7 +718,7 @@ export default function TodoList({ todos, theme, tab }) { } ``` -```js src/List.js +```js {expectedErrors: {'react-compiler': [5, 6]}} src/List.js import { memo } from 'react'; const List = memo(function List({ items }) { @@ -855,7 +856,7 @@ export default function TodoList({ todos, theme, tab }) { } ``` -```js src/List.js +```js {expectedErrors: {'react-compiler': [5, 6]}} src/List.js import { memo } from 'react'; const List = memo(function List({ items }) { @@ -1127,7 +1128,7 @@ function ChatRoom({ roomId }) { serverUrl: 'https://localhost:1234', roomId: roomId } - + const connection = createConnection(options); connection.connect(); return () => connection.disconnect(); @@ -1371,7 +1372,7 @@ When you find which dependency breaks memoization, either find a way to remove i Suppose the `Chart` component is wrapped in [`memo`](/reference/react/memo). You want to skip re-rendering every `Chart` in the list when the `ReportList` component re-renders. However, you can't call `useMemo` in a loop: -```js {5-11} +```js {expectedErrors: {'react-compiler': [6]}} {5-11} function ReportList({ items }) { return (
diff --git a/src/content/reference/react/useRef.md b/src/content/reference/react/useRef.md index 8ab53aef3..e87ef6b8a 100644 --- a/src/content/reference/react/useRef.md +++ b/src/content/reference/react/useRef.md @@ -197,7 +197,7 @@ React expects that the body of your component [behaves like a pure function](/le Reading or writing a ref **during rendering** breaks these expectations. -```js {3-4,6-7} +```js {expectedErrors: {'react-compiler': [4]}} {3-4,6-7} function MyComponent() { // ... // 🚩 Don't write a ref during rendering diff --git a/src/content/reference/react/useTransition.md b/src/content/reference/react/useTransition.md index 527078143..923b6c0c9 100644 --- a/src/content/reference/react/useTransition.md +++ b/src/content/reference/react/useTransition.md @@ -161,7 +161,7 @@ function CheckoutForm() { } ``` -The function passed to `startTransition` is called the "Action". You can update state and (optionally) perform side effects within an Action, and the work will be done in the background without blocking user interactions on the page. A Transition can include multiple Actions, and while a Transition is in progress, your UI stays responsive. For example, if the user clicks a tab but then changes their mind and clicks another tab, the second click will be immediately handled without waiting for the first update to finish. +The function passed to `startTransition` is called the "Action". You can update state and (optionally) perform side effects within an Action, and the work will be done in the background without blocking user interactions on the page. A Transition can include multiple Actions, and while a Transition is in progress, your UI stays responsive. For example, if the user clicks a tab but then changes their mind and clicks another tab, the second click will be immediately handled without waiting for the first update to finish. To give the user feedback about in-progress Transitions, the `isPending` state switches to `true` at the first call to `startTransition`, and stays `true` until all Actions complete and the final state is shown to the user. Transitions ensure side effects in Actions to complete in order to [prevent unwanted loading indicators](#preventing-unwanted-loading-indicators), and you can provide immediate feedback while the Transition is in progress with `useOptimistic`. @@ -597,7 +597,7 @@ export default function TabButton({ action, children, isActive }) {