diff --git a/src/components/MDX/Sandpack/Preview.tsx b/src/components/MDX/Sandpack/Preview.tsx index ead9341b6e3..38a6fc03171 100644 --- a/src/components/MDX/Sandpack/Preview.tsx +++ b/src/components/MDX/Sandpack/Preview.tsx @@ -99,10 +99,13 @@ export function Preview({ useEffect(function createBundler() { const iframeElement = iframeRef.current!; - registerBundler(iframeElement, clientId); - + if (!iframeElement.dataset.registered) { + registerBundler(iframeElement, clientId); + iframeElement.dataset.registered = 'true'; + } return () => { unregisterBundler(clientId); + iframeElement.dataset.registered = ''; }; }, []); diff --git a/src/content/learn/conditional-rendering.md b/src/content/learn/conditional-rendering.md index 95be5d2e018..fdb278667b0 100644 --- a/src/content/learn/conditional-rendering.md +++ b/src/content/learn/conditional-rendering.md @@ -675,26 +675,30 @@ There are multiple ways you could go about this, but here is one starting point: ```js function Drink({ name }) { - let part, caffeine, age; - if (name === 'tea') { - part = 'leaf'; - caffeine = '15–70 mg/cup'; - age = '4,000+ years'; - } else if (name === 'coffee') { - part = 'bean'; - caffeine = '80–185 mg/cup'; - age = '1,000+ years'; - } + const drinks = { + tea: { + part: 'leaf', + caffeine: '15–70 mg/cup', + age: '4,000+ years' + }, + coffee: { + part: 'bean', + caffeine: '80–185 mg/cup', + age: '1,000+ years' + } + }; + + const info = drinks[name]; return (

{name}

Part of plant
-
{part}
+
{info.part}
Caffeine content
-
{caffeine}
+
{info.caffeine}
Age
-
{age}
+
{info.age}
); 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 39a1dc74045..c9030a159e9 100644 --- a/src/content/learn/you-might-not-need-an-effect.md +++ b/src/content/learn/you-might-not-need-an-effect.md @@ -1272,7 +1272,9 @@ input { margin-top: 10px; } -This approach satisfies the requirements too. When you type into the input, only the `text` state variable updates. Since the `text` state variable is in the child `NewTodo` component, the parent `TodoList` component won't get re-rendered. This is why `getVisibleTodos()` doesn't get called when you type. (It would still be called if the `TodoList` re-renders for another reason.) +This approach works because `getVisibleTodos()` is only called when its dependencies (`todos` or `showActive`) change. In the original implementation, typing in the input doesn’t trigger additional calls because the `text` state is local to the `NewTodo` component and not part of the dependencies. + +The key idea is that both the initial and extracted versions avoid unnecessary recomputations—the update to `text` does not cause `getVisibleTodos()` to run. Extracting `NewTodo` mainly improves clarity by keeping the input state isolated, not by changing performance behavior.