1

This code work, but VSCode return error.

ReactDOM.render(
	 <span  style={{"--backlight-color": "red"}} /* change color of span */ >text</span>,
	document.getElementById('root')
);
:root {
  --backlight-color: silver; /* default color of span*/
}

span {
  background-color: var(--backlight-color);
}
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.min.js"></script>

This error is on line <span style={{"--backlight-color": "red"}} >text</span>,:

Types of property 'style' are incompatible. Type '{ "--backlight-color": any; }' is not assignable to type 'CSSProperties'. Object literal may only specify known properties, and '"--backlight-color"' does not exist in type 'CSSProperties'.

Does VSCode support CSS variables in react?
How else should you implement it?

I do not want it to display this error in VScode.

It's about this bug: https://github.com/facebook/react/issues/6411

1
  • 1
    set background-color:silver insteadd of --backlight-color: silver; Commented Jun 7, 2018 at 11:17

2 Answers 2

0

You have to set

root {
  background-color:silver;
}

instead

root {
  --backlight-color: silver;
}
Sign up to request clarification or add additional context in comments.

2 Comments

tell me for more help
No. This is default color of span. It's about the error VSCode shows.
0

It seems this is still an issue today. You can work around it by typecasting the custom property:

<span style={{ "--backlight-color": "red"}: as React.CSSProperties}>text</span>

credit: https://stackoverflow.com/a/54128069/1783174)

If you need this in more then one place, I suggest you add it to the TS definitions as described here: https://stackoverflow.com/a/70398145/1783174

1 Comment

I won't check because I haven't been using React lately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.