You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched the issues of this repository and believe that this is not a duplicate.
When defining a variable with css outside of React class components there are no problems:
constbackgroundImageStyle=css` div { background-color: red; }`;classDropdownextendsComponent{render(){return(<div><stylejsx>{backgroundImageStyle}</style></div>);}}exportdefaultDropdown;
But when moving the style definition into render() like this, it doesn't work:
render(){constbackgroundImageStyle=css` div { background-color: red; } `;return(<div><stylejsx>{backgroundImageStyle}</style></div>);}
It gives me this error message:
StyleSheet: `insertRule` accepts only strings.
Error: StyleSheet: `insertRule` accepts only strings.
at invariant (node_modules/styled-jsx/dist/lib/stylesheet.js:274:11)
at StyleSheet.insertRule (node_modules/styled-jsx/dist/lib/stylesheet.js:125:7)
at node_modules/styled-jsx/dist/stylesheet-registry.js:88:29
at Array.map (native)
at StyleSheetRegistry.add (node_modules/styled-jsx/dist/stylesheet-registry.js:87:27)
at JSXStyle.componentWillMount (node_modules/styled-jsx/dist/style.js:58:26)
at resolve (node_modules/react-dom/cjs/react-dom-server.node.development.js:2119:12)
at ReactDOMServerRenderer.render (node_modules/react-dom/cjs/react-dom-server.node.development.js:2260:22)
at ReactDOMServerRenderer.read (node_modules/react-dom/cjs/react-dom-server.node.development.js:2234:19)
at renderToString (node_modules/react-dom/cjs/react-dom-server.node.development.js:2501:25)
Why is that so and is there a way to fix this? The reason why I want to define the style within render() is because I want to modify it based on props coming in:
render(){letbackgroundImageStyle;if(this.props.bgcolor){backgroundImageStyle=css` div { background-color: ${this.props.bgcolor}; } `;}else{backgroundImageStyle=css` div { background-color: red; } `;}return(<div><stylejsx>{backgroundImageStyle}</style></div>);}
The text was updated successfully, but these errors were encountered:
Thanks again @giuseppeg for your quick response. To bad there is no support for this. I wanted to avoid inline styles as much as possible and put all the css code into variables :-(
I still don't understand why this doesn't work, I mean there are no props in this example:
render(){constbackgroundImageStyle=css` div { background-color: red; } `;return(<div><stylejsx>{backgroundImageStyle}</style></div>);}
When defining a variable with css outside of React class components there are no problems:
But when moving the style definition into
render()
like this, it doesn't work:It gives me this error message:
Why is that so and is there a way to fix this? The reason why I want to define the style within
render()
is because I want to modify it based on props coming in:The text was updated successfully, but these errors were encountered: