【问题标题】:how to set inline style in react jsx如何在 React jsx 中设置内联样式
【发布时间】:2017-07-25 19:48:16
【问题描述】:

我正在尝试在 react 组件中应用内联样式,如下所示。这是设置样式的正确方法吗,如果不是,我该如何实现?

  <div
    {...customProps}
    style={{ width: `${widthValue}%`,
      animationDelay: `${animationDelay}s`,
      zIndex: `${zIndex}`,
      animation: `loadbar ${incrementValue}s linear forwards`}}
    key={i}
    className={`well-background--${group.concept}
      well-GroupedProgressBar--progress
      well-GroupedProgressBar--${props.heightSize}`}
  />,

谢谢

【问题讨论】:

  • 你是如何测试这个的?
  • 我相信这是正确的做法。正如其他答案所建议的那样,您可以为其使用自定义变量,但是您的解决方案应该可以正常工作。

标签: reactjs jsx


【解决方案1】:

您需要将 obj 传递给内联样式属性。你的代码是正确的,但是你可以通过在渲染函数中准备样式对象来清理你的代码,如果它每次都改变的话

render() {
    var style = {
      width: `${widthValue}%`,
      animationDelay: `${animationDelay}s`,
      zIndex: `${zIndex}`,
      animation: `loadbar ${incrementValue}s linear forwards`
    }
    return (
        <div
           {...customProps}
           style={style}
           key={i}
           className={`well-background--${group.concept}
                        well-GroupedProgressBar--progress
                       well-GroupedProgressBar--${props.heightSize}`
                      }
          />

    )
}

【讨论】:

  • 你做了和他基本一样的事情,唯一不同的是你使用了变量;)
  • @ShubhamKhatri,我确实尝试过,但我没有看到任何区别。问题是animation: loadbar ${incrementValue}s 线性转发属性没有得到应用所以我想如果我做错了什么
猜你喜欢
  • 2021-05-18
  • 2022-06-12
  • 2018-03-01
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
  • 2021-05-08
相关资源
最近更新 更多