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

fix: Make JSXStyle return a noop if the registry context is not provided #749

Merged
merged 1 commit into from
Sep 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/style.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { useLayoutEffect, useContext } from 'react'
import { StyleSheetContext } from './stylesheet-registry'
import { useLayoutEffect } from 'react'
import { useStyleRegistry } from './stylesheet-registry'
import { computeId } from './lib/hash'
export default function JSXStyle(props) {
const registry = useContext(StyleSheetContext)
const registry = useStyleRegistry()

// If `registry` does not exist, we do nothing here.
if (!registry) {
return null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following is just an observation and me asking out of curiosity, not a big deal:

Normally I throw an error here to help users understand what's going on. In the case you described in the PR description can't you remove the babel plugin altogether so that style tags are not turned into JSXStyle elements?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In certain use cases, for example a lib that renders a component (which has styled-jsx inside) with renderToStaticMarkup. We can't throw since it should not block rendering, and we can't ask them to disable the babel plugin as well because it's not a project level thing.

}

if (typeof window === 'undefined') {
registry.add(props)
return null
}

useLayoutEffect(() => {
registry.add(props)
return () => {
registry.remove(props)
}
// props.children can be string[], will be striped since id is identical
}, [props.id, String(props.dynamic)])

return null
}

Expand Down