From c0b8639485529015421399e5ff7963d6592a8b98 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 20 Dec 2021 16:06:02 +0100 Subject: [PATCH] perf: fallback to module level registry in browser --- src/style.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/style.js b/src/style.js index db78742f..405498ff 100644 --- a/src/style.js +++ b/src/style.js @@ -1,19 +1,22 @@ import React from 'react' -import { useStyleRegistry } from './stylesheet-registry' +import { useStyleRegistry, createStyleRegistry } from './stylesheet-registry' import { computeId } from './lib/hash' // Opt-into the new `useInsertionEffect` API in React 18, fallback to `useLayoutEffect`. // https://github.com/reactwg/react-18/discussions/110 const useInsertionEffect = React.useInsertionEffect || React.useLayoutEffect + +const isBrowser = typeof window !== 'undefined' +const defaultRegistry = createStyleRegistry() export default function JSXStyle(props) { - const registry = useStyleRegistry() + const registry = isBrowser ? defaultRegistry : useStyleRegistry() // If `registry` does not exist, we do nothing here. if (!registry) { return null } - if (typeof window === 'undefined') { + if (!isBrowser) { registry.add(props) return null }