admin管理员组文章数量:1357402
In the code below I've created a button ponent with the help of React. Since React allows you to create reusable ponents, I wanted to be able to create a button that could contain different text and be a different color. For example:
import React from "react"
import App from "../GlowButton/GlowButton"
const App= () => {
return (
<div className="App">
<GlowButton text="Darth vader" color="red"/>
<GlowButton text="Yoda" color ="green"/>
<GlowButton text="Obi Wan Kenobi" color="aqua"/>
</div>
)
}
export default App
I've gotten the different text part to work, but I am not sure how (or if it's possible) to change the color of the ponent.
The css file is:
:root {
--buttonColor: orange;
}
.glowButton {
margin: auto 0;
}
.myButton {
border: none;
padding: 0.4em 0.8em;
text-align: center;
background-color: transparent;
color: var(--buttonColor);
letter-spacing: 0.05rem;
font-family: 'Poppins', sans-serif;
font-size: 1.1rem;
border: var(--buttonColor) 0.2em solid ;
border-radius: 0.45em;
transition: 0.5s;
text-shadow: 1px 2px 5px;
box-shadow: 0 0 0.1em var(--buttonColor), 0 0 0.3em var(--buttonColor);
&:hover{
color: black;
background-color: var(--buttonColor);
box-shadow: 0 0 0.5em var(--buttonColor), 0 0 0.7em var(--buttonColor), 0 0 1.10em var(--buttonColor);
}
}
The styles and specifics of the css file don't really matter all that much. The key takeaway is that it creates a button based on the --buttonColor variable.
The JS/JSX portion is:
import React from "react"
import "./GlowButton.scss"
// Properties are color and text
const GlowButton = (props) => {
return (
<inline className="glowButton">
<button className="myButton">{props.text}</button>
</inline>
)
}
export default GlowButton
From looking online I've found this section of code that can select and change a variable:
// change the button according to users props
const root = document.querySelector(':root')
root.style.setProperty('--buttonColor', COLORCONST)
If COLORCONST is set to a single color then the code works fine, but if it is set to props.color it is not valid, ignoring the color and displaying the default orange in the css file.
It would be great if someone could explain what I'm doing wrong with props.color or why I can't use it.
In the code below I've created a button ponent with the help of React. Since React allows you to create reusable ponents, I wanted to be able to create a button that could contain different text and be a different color. For example:
import React from "react"
import App from "../GlowButton/GlowButton"
const App= () => {
return (
<div className="App">
<GlowButton text="Darth vader" color="red"/>
<GlowButton text="Yoda" color ="green"/>
<GlowButton text="Obi Wan Kenobi" color="aqua"/>
</div>
)
}
export default App
I've gotten the different text part to work, but I am not sure how (or if it's possible) to change the color of the ponent.
The css file is:
:root {
--buttonColor: orange;
}
.glowButton {
margin: auto 0;
}
.myButton {
border: none;
padding: 0.4em 0.8em;
text-align: center;
background-color: transparent;
color: var(--buttonColor);
letter-spacing: 0.05rem;
font-family: 'Poppins', sans-serif;
font-size: 1.1rem;
border: var(--buttonColor) 0.2em solid ;
border-radius: 0.45em;
transition: 0.5s;
text-shadow: 1px 2px 5px;
box-shadow: 0 0 0.1em var(--buttonColor), 0 0 0.3em var(--buttonColor);
&:hover{
color: black;
background-color: var(--buttonColor);
box-shadow: 0 0 0.5em var(--buttonColor), 0 0 0.7em var(--buttonColor), 0 0 1.10em var(--buttonColor);
}
}
The styles and specifics of the css file don't really matter all that much. The key takeaway is that it creates a button based on the --buttonColor variable.
The JS/JSX portion is:
import React from "react"
import "./GlowButton.scss"
// Properties are color and text
const GlowButton = (props) => {
return (
<inline className="glowButton">
<button className="myButton">{props.text}</button>
</inline>
)
}
export default GlowButton
From looking online I've found this section of code that can select and change a variable:
// change the button according to users props
const root = document.querySelector(':root')
root.style.setProperty('--buttonColor', COLORCONST)
If COLORCONST is set to a single color then the code works fine, but if it is set to props.color it is not valid, ignoring the color and displaying the default orange in the css file.
It would be great if someone could explain what I'm doing wrong with props.color or why I can't use it.
Share Improve this question edited May 6, 2021 at 1:31 10 Rep 2,2707 gold badges21 silver badges33 bronze badges asked May 5, 2021 at 23:43 HelloThereHelloThere 431 silver badge3 bronze badges2 Answers
Reset to default 5What you can do is append a color based class dynamically like,
import React from "react"
import "./GlowButton.scss"
// Properties are color and text
const GlowButton = (props) => {
return (
<inline className="glowButton">
<button className={`myButton ${props.color}`}>{props.text}</button>
</inline>
)
}
export default GlowButton
And then add conditional styling to your stylesheet like,
.myButton {
//Common Styles Here
&.red {
//Distinct Styles Here
}
&.green {
//Distinct Styles Here
}
&.aqua {
//Distinct Styles Here
}
}
you need something like this:
const GlowButton = (props) => {
return (
<inline className="glowButton">
<button className="myButton" style={{color: props.color}}>{props.text}</button>
</inline>
)}
本文标签: javascriptHow do you change the color of a React component using propsStack Overflow
版权声明:本文标题:javascript - How do you change the color of a React component using props - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743967048a2570079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论