From f77099b6f1ccc658eff3467c6b9337e1b77ec854 Mon Sep 17 00:00:00 2001 From: Ricky Date: Wed, 22 Mar 2023 13:33:48 -0400 Subject: [PATCH] Remove layout effect warning on the server (#26395) ## Overview This PR unfortunately removes the warning emitted when using layout effects on the server: > useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client. See https://reactjs.org/link/uselayouteffect-ssr for common fixes. ## Why this warning exists The new docs explain this really well. Adding a screenshot because as part of this change, we'll be removing these docs. Screenshot 2023-03-15 at 10 56 17 AM ## Why are we changing it In practice, users are not just ignoring this warning, but creating hooks to bypass this warning by switching the useLayoutEffect hook on the server instead of fixing it. This battle seems to be lost, so let's remove the warning so at least users don't need to use the indirection hook any more. In practice, if it's an issue, you should see the problems like flashing the wrong content on first load in development. --- ...MServerSelectiveHydration-test.internal.js | 8 +------- packages/react-server/src/ReactFizzHooks.js | 19 +------------------ 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactDOMServerSelectiveHydration-test.internal.js b/packages/react-dom/src/__tests__/ReactDOMServerSelectiveHydration-test.internal.js index 275c3e8fdefd7..6b51c9a189088 100644 --- a/packages/react-dom/src/__tests__/ReactDOMServerSelectiveHydration-test.internal.js +++ b/packages/react-dom/src/__tests__/ReactDOMServerSelectiveHydration-test.internal.js @@ -1607,13 +1607,7 @@ describe('ReactDOMServerSelectiveHydration', () => { ); } - let finalHTML; - expect(() => { - finalHTML = ReactDOMServer.renderToString(); - }).toErrorDev([ - 'useLayoutEffect does nothing on the server', - 'useLayoutEffect does nothing on the server', - ]); + const finalHTML = ReactDOMServer.renderToString(); assertLog(['App', 'A', 'B']); diff --git a/packages/react-server/src/ReactFizzHooks.js b/packages/react-server/src/ReactFizzHooks.js index bf236793bcee1..be4b32e8f797b 100644 --- a/packages/react-server/src/ReactFizzHooks.js +++ b/packages/react-server/src/ReactFizzHooks.js @@ -440,23 +440,6 @@ function useRef(initialValue: T): {current: T} { } } -export function useLayoutEffect( - create: () => (() => void) | void, - inputs: Array | void | null, -) { - if (__DEV__) { - currentHookNameInDev = 'useLayoutEffect'; - console.error( - 'useLayoutEffect does nothing on the server, because its effect cannot ' + - "be encoded into the server renderer's output format. This will lead " + - 'to a mismatch between the initial, non-hydrated UI and the intended ' + - 'UI. To avoid this, useLayoutEffect should only be used in ' + - 'components that render exclusively on the client. ' + - 'See https://reactjs.org/link/uselayouteffect-ssr for common fixes.', - ); - } -} - function dispatchAction( componentIdentity: Object, queue: UpdateQueue, @@ -633,7 +616,7 @@ export const HooksDispatcher: Dispatcher = { useRef, useState, useInsertionEffect: noop, - useLayoutEffect, + useLayoutEffect: noop, useCallback, // useImperativeHandle is not run in the server environment useImperativeHandle: noop,