【问题标题】:extending react style prop with typescript用打字稿扩展反应风格道具
【发布时间】:2022-01-22 16:15:33
【问题描述】:

在 react 中扩展 style prop 以接受 css 变量作为键的最佳方法是什么?

<button style={{ "--color": "red", ...props.style }} />

【问题讨论】:

    标签: css reactjs typescript


    【解决方案1】:

    您可以执行以下操作:

     render() {
       var style = {  "--color": "red", ...props.style } as React.CSSProperties;
       return (<button style={style} />);
      }
    

    更多React.CSSProperties类型定义:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/8871b1e8938a1ed698ec8a88c77ee169294e45d4/types/react/index.d.ts#L974-L983

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2020-08-21
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      相关资源
      最近更新 更多