next.js: Using styled-jsx in a variable throws warning "Received `true` for a non-boolean attribute `jsx`"
- I have searched the issues of this repository and believe that this is not a duplicate.
I wanted to move styled-jsx code to a dedicated variable and then inject it into the render() method like this:
const divstyle = (
<style jsx>{`
div {
color: red;
}
`}
</style>
);
return (
<div>
<div>foo</div>
{divstyle}
</div>
);
This will inject the style into the DOM but not scoped. In console I see this error:
Warning: Received `true` for a non-boolean attribute `jsx`.
If you want to write it to the DOM, pass a string instead: jsx="true" or jsx={value.toString()}.
If I move the <style jsx> part from the variable back into the render method, everything works perfectly.
What’s wrong? Isn’t it possible to to use styled-jsx outside of render() ?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 21 (9 by maintainers)
Commits related to this issue
- Fix styled-jsx <style jsx> boolean property issue Ref: https://github.com/zeit/next.js/issues/3432#issuecomment-423063539 — committed to answerfamily/answerfamily-web by johnson-liang-appier 8 years ago
I found the same issue with the new feature of React empty jsx tag:
The warning comes back:
Is there any plan to fix it 😆
use
<React.Frament>for now, styled-jsx is not Babel7 compatible yet. We’ll add support for that in styled-jsx v3@Stephen2 you mean the issue with fragments? If so yes, I haven’t had the time to add fragments support yet. I will try to do that this week or the next one:)
Whenever you see that error it means that styled-jsx didn’t transpile your code at all therefore the
styleelement is rendered as regular html andjsxis an invalid prop for it.If you want to extract your styles to a variable you can use
cssfromstyled-jsx/csslike so:However I wouldn’t recommend that since
cssproduces both scoped and global styles. We need to optimize that in the future so that styles like that are tree-shakeable and tools can do dead code elimination.This looks bad news and messy…
I thought that using styled-jsx (that looks nice for me and that comes by default) would prevent from having to hack.
Is there something planned to fix that? Otherwise the best and cleaner solution for me will be to move to another css-in-js like
styled-components(I don’t really want, I found next js default solution nice enough on the paper…) 😔.@valnub that feature is available with styled-jsx@2.2.1 which is included with Next@4.3.0-canary.1