Skip to content

Commit

Permalink
feat: opt in insertion effect hook when available (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 20, 2022
1 parent dcadf5d commit 6e224a9
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/style.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useLayoutEffect } from 'react'
import React from 'react'
import { useStyleRegistry } 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
export default function JSXStyle(props) {
const registry = useStyleRegistry()

Expand All @@ -14,7 +18,7 @@ export default function JSXStyle(props) {
return null
}

useLayoutEffect(() => {
useInsertionEffect(() => {
registry.add(props)
return () => {
registry.remove(props)
Expand Down

0 comments on commit 6e224a9

Please sign in to comment.