简体   繁体   English

如何使用 JSX 将 JavaScript 变量传递到 CSS 中?

[英]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.colortag.color

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用map函数内的JSX中的变量更改内联CSS样式? - How do I change inline CSS styles using a variable in JSX inside a map function? 我如何使用 javascript 将值从变量传递给 .html() - How do i pass the value from a variable to .html() using javascript 如何比较 javascript 中的 CSS 变量? - How do I compare a CSS variable in javascript? 如何将JavaScript确认变量传递给php变量 - how do i pass JavaScript confirm variable to php variable 从 HTML,CSS,JS 转换为 JSX, Z2C56C3602580420DFBEDZ,JS 时,如何在反应中链接我的 javascript 文件? - How do I link my javascript files in react when converting from HTML,CSS,JS into JSX, CSS, JS? 如何在ReactJs中传递css导入中的变量 - How do I pass variable in css import in ReactJs 如何使用 Javascript 和 Z3EB7106C3477A60E6BBF5DBFDE1DA 将 HTML 单选按钮值传递给 PHP 变量? - How do I pass HTML radio button value to a PHP variable using Javascript and Ajax? 如何使用 Laravel 将变量传递给 JavaScript 以便在私人频道上广播? - How do I pass a variable to JavaScript using Laravel in order to broadcast on a private channel? 如何使用JavaScript将存储在变量中的textarea的值传递给div - How do i pass the value of textarea which is stored in a variable into a div using the JavaScript 如何使用 EJS 模板引擎将变量传递给内联 javascript? - How do I pass a variable to inline javascript using EJS templating engine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM