React Styled Component 的 CSS 方法优化

维克多

哪个更优化,为什么?

我听说cssStyled-component 的方法非常昂贵,但我想知道多个嵌套${(prop) => {}}是否比css使用内部方法的单个 fn 更昂贵

方法一

const Foo = styled.div`
  border-radius: 50%;
  display: flex;
  width: ${(props) => props.size};
  height: ${(props) => props.size};
  color: ${(props) => props.bgColor};
  background-color: ${(props) => props.bgColor};
`;

对比

方法二

const Bar = styled.div`
  border-radius: 50%;
  display: flex;
  ${(props) => {
    return css`
      width: ${props.size};
      height: ${props.size};
      color: ${props.bgColor};
      background-color: ${props.bgColor};
    `;
  }}
`;
里卡多·洛佩斯

性能差异应该可以忽略不计。样式化组件唯一的优化是如果您的样式是静态的(没有插值)。

请注意,styled.div或任何其他样式化 CSS 方法已经执行与css. 这些方法接受函数作为参数(而不是内插字符串)。因此,您可以更进一步并执行以下操作:

const Baz = styled.div((props) => css`
  border-radius: 50%;
  display: flex;
  width: ${props.size};
  height: ${props.size};
  color: ${props.bgColor};
  background-color: ${props.bgColor};
`);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 styled-component 修改 css react 组件

如何将 React Functional Component 属性的类型设置为 Styled Component`styled.div`?

React Styled Component SVG 样式显示不正确?

styled-components表示在您的React组件(Component)周围包装了styled()

React Styled-Components CSS向子元素添加!important

如何在React上的Styled Component画布上正确使用TypeScript类型

无法在 React Styled Component 输入中定位最后一个类型

用React Styled-Components导入scss变量的最佳方法?

react component is unable to locate my CSS class

React + Styled Components-分离并重复使用CSS“捆绑包”

Storybook + Ionic React + styled-components CSS 順序問題

React Native UI Component方法

ES6 + React Component实例方法

React Styled-Components 传递道具问题

React Styled 组件未显示正确的输出

反应如何设置样式:styled-dropdown-component

React Component或React PureComponent

Styled-components vs Emotion - 如何在 Styled-components 上重新应用 css`style` 函数

扩展React.Component与Component

React Component render方法中的语法错误

React只将Component方法绑定到此-解决吗?

如何对React Component的shouldComponentUpdate方法进行单元测试

组件具有渲染方法,但不扩展React.Component

为iOS /移动Safari优化CSS 3动画的正确方法?

如何在使用react-data-table-component时更改主标题css属性

React Styled Components-如何访问原始HTML

React Styled-Components:参考其他组件

使用 react 和 styled 组件打印时如何隐藏按钮?

nextjs + react-native-web + styled-components:warnOnce