[英]How do I pass in a JavaScript variable into CSS using JSX?
I understand that CSS can't access JavaScript variables, I have tried creating the react object in an HTML tag, and then tried modifying the style by using this method:我知道 CSS 无法访问 JavaScript 变量,我尝试在 HTML 标记中创建 react 对象,然后尝试使用以下方法修改样式:
element.style.property = value element.style.property = 值
However, I create my react object in a return statement, and because I try to modify the object after the return statement, it is essentially ignored.但是,我在 return 语句中创建了我的 react 对象,并且因为我尝试在 return 语句之后修改该对象,所以它基本上被忽略了。
The element being mapped over is an array of objects, and I would like to access one of the values from the objects.被映射的元素是一个对象数组,我想访问对象中的一个值。
My description is not very good;我的描述不是很好; hopefully a snipped of my code will better showcase my issue.希望我的代码片段能更好地展示我的问题。
export default function Chips({tags}) {
const classes = useStyles();
const handleDelete = () => {
console.info('You clicked the delete icon.');
};
const handleClick = () => {
console.info('You clicked the Chip.');
};
return (
<div className={classes.root}>
{tags.map(tag => {
const tagcolor = String(tag.color);
return (
<Chip
id="chip"
className={classes.chip}
icon={<Icon id="icon" style={{backgroundColor = {tag.color}, borderRadius: '50%'}}/>}
label={tag.title}
onClick={handleClick}
/>
)
document.getElementById("icon").style.backgroundColor = tag.color;
})}
</div>
);
}
The specific issue is that I would like to change the Chip icon "backgroundColor" style attribute according to the value passed in from the object, but CSS cannot access it, so the backgroundColor is never set.具体问题是我想根据从对象传入的值更改芯片图标“backgroundColor”样式属性,但CSS无法访问它,因此从未设置backgroundColor。
You're on the right track, just have some syntax errors.你在正确的轨道上,只是有一些语法错误。
The style
prop is meant to be an object , which is why you have the double curly braces {{
and }}
closing it out. style
prop 是一个object ,这就是为什么你用双花括号{{
和}}
将它关闭。 The outer braces are for the JSX , the inner are for the object.外部大括号用于 JSX ,内部大括号用于对象。 Meaning, instead of:意思是,而不是:
style={{backgroundColor = {tag.color}, borderRadius: '50%'}}
You can do:你可以做:
style={{backgroundColor:tag.color, borderRadius: '50%'}}
No equals sign, because backgroundColor
.etc are meant to be like object keys, which are assigned in this syntax using :
not =
.没有等号,因为backgroundColor
.etc 就像对象键一样,在此语法中使用:
not =
分配。 The tag.color
also doesn't need the JSX curly braces around it, as they are already in the first JSX {
braces and will be interpreted correctly as your variable already. tag.color
也不需要围绕它的 JSX 花括号,因为它们已经在第一个 JSX {
大括号中,并且已经被正确解释为您的变量。
As an aside, note that using the style
attribute is not really recommended .顺便说一句,请注意,实际上并不推荐使用style
属性。 Secondly, note on your <Icon>
component as you've done it there, that style
will be passed as a prop.其次,请注意您的<Icon>
组件,因为您在那里完成了它,该style
将作为道具传递。 To access it and make it render in HTML/CSS, you'll have to do something like props.style
and put that in a render or return method to attach those stylings to HTML.要访问它并使其在 HTML/CSS 中呈现,您必须执行类似props.style
并将其放入 render 或 return 方法以将这些props.style
附加到 HTML。
I've made an example of that for you here: https://codesandbox.io/s/react-playground-forked-rzm2d?file=/Icon.js我在这里为你做了一个例子: https : //codesandbox.io/s/react-playground-forked-rzm2d?file=/Icon.js
Note it's an object, so I'm just spreading it into the existing <Icon/>
styles so you can make it more component driven (as an example).请注意,它是一个对象,所以我只是将它扩展到现有的<Icon/>
样式中,以便您可以使其更加组件驱动(作为示例)。 In this way, you can separate more static styles (like borderRadius
) from dynamic styles (like those derived from tag.color
).通过这种方式,您可以将更多静态样式(如borderRadius
)与动态样式(如从tag.color
派生的tag.color
) tag.color
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.