1、定义变量

const divStyle: React.CSSProperties = {
    width: "11rem",
    height: "7rem",
    backgroundColor: `rgb(${props.color.red},${props.color.green}, ${props.color.blue})`
  };

使用:

<div style={divStyle} />

2、定义函数

interface SidebarProps {
    isVisible: boolean;
}

const divStyle = (props: SidebarProps): React.CSSProperties=>({
    width: props.isVisible ? "23rem" : "0rem"
})

export const SidebarComponent: React.StatelessComponent<SidebarProps> = props => (
    <div style={divStyle(props)}>
    {props.isVisible}
    </div>
)
const divStyle = (props: SidebarProps): React.CSSProperties=>({
    width: props.isVisible ? "23rem" : "0rem"
})

返回值为React.CSSProperties类型

 

相关文章:

  • 2018-10-29
  • 2022-12-23
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2021-11-18
  • 2021-11-14
猜你喜欢
  • 2021-11-29
  • 2021-08-12
  • 2021-11-16
  • 2021-11-23
相关资源
相似解决方案