Here is my code:
ReactDOM.render(
<h1 style = {{color:"red"}}>Foo</h1>,
document.getElementById("root")
)
How come this works ^ but this doesn't:
var styles = {
color:"red"
}
ReactDOM.render(
<h1 style = {{styles}}>Foo</h1>,
document.getElementById("root")
)
I want to style easily without having to type each style out every time i create a new DOM element...
Use single brackets in your render. Like this:
ReactDOM.render(
<h1 style = {styles}>Foo</h1>,
document.getElementById("root")
)
The reason style={{ styles }}
doesn't work is because it's the same as style={{ styles: styles }}
, which passes along an object with a styles
property, instead of the actual styles you defined for it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.