From d5c06fc5cfce0010e32e5fb2417323d9e75c1f9f Mon Sep 17 00:00:00 2001 From: RTrace Date: Mon, 2 Sep 2024 04:23:17 -0700 Subject: [PATCH] docs: add useLayoutEffect warning mitigation (#944) --- docs/guides/migrating-to-v2.mdx | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/guides/migrating-to-v2.mdx b/docs/guides/migrating-to-v2.mdx index 3a874e11..6cad23ae 100644 --- a/docs/guides/migrating-to-v2.mdx +++ b/docs/guides/migrating-to-v2.mdx @@ -102,6 +102,19 @@ state.obj = deepClone(newObj) // and do something later with `newObj` ``` +### `useLayoutEffect` server warning + +If you're using React 18 with SSR, add this conditional to prevent excessive warnings. + +```js +import { snapshot, useSnapshot as useSnapshotOrig } from 'valtio' + +const isSSR = typeof window === 'undefined' +export const useSnapshot = isSSR ? (p) => snapshot(p) : useSnapshotOrig + +// render with `useSnapshot` as usual +``` + ## Links - https://github.com/pmndrs/valtio/discussions/703