Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: opt in insertion effect hook when available #753

Merged
merged 4 commits into from Oct 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/style.js
@@ -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
huozhi marked this conversation as resolved.
Show resolved Hide resolved
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