【问题标题】:How to use styled-components in react component如何在反应组件中使用样式组件
【发布时间】:2020-05-12 09:27:36
【问题描述】:

使用styled-components 的新手总数。我想知道它的用途是什么?样式化后我应该如何实现组件生命周期方法?为简单起见,我删除了所有其他样式。

import styled from 'styled-components';

const Button = styled.button`
  background-color: 'green'
`

export default Button;

我想知道如何进一步处理这个Button 组件?

传统上我们可以声明一个基于类的组件并实现一些生命周期方法,但现在有了这个styled-components,我不确定如何将它们组合在一起,因为它们实际上是单个Button 组件?

更新:

Button.js 的完整源代码。通过下面的代码,所有样式都将消失,我无法理解问题

import React from 'react';
import styled from 'styled-components';
// import Button from 'react-bootstrap/Button';

import color from '../config/color';

const Button = ({ children, onPress }) => (
  <button type="button" onPress={onPress}>{children}</button>
);

const StyledButton = styled(Button)`
  width: 12rem;
  height: 54px;
  font-size: 1rem;
  background-color: ${(props) => {
    if (props.inverted) return 'white';
    if (props.disabled) return color.disabled;
    return (props.color || color.primary);
  }};
  color: ${(props) => {
    if (props.disabled) return color.disabledText;
    if (props.inverted) return (props.color || color.primary);
    return 'white';
  }};
  border:${(props) => (props.inverted ? `2px solid ${props.color || color.primary}` : 'none')};
  border-radius: 60px;

  &:hover {
    filter: ${(props) => (props.inverted || props.disabled ? 'none' : 'brightness(95%)')}
  }
`;

export default StyledButton;

【问题讨论】:

  • Hmmm.. 你想用这个组件中的样式按钮做什么?
  • @wentjun:我想我不太理解这个概念。传统上我会在Button.js 中定义Button 组件。我正在考虑使用styled-components 来设置样式?
  • 您可以像使用任何其他组件一样使用它..!让我写一个例子。

标签: javascript css reactjs styled-components


【解决方案1】:

为了设置自定义反应组件的样式,您可以将自定义组件名称作为参数传递给styled。根据文档:

styled 方法完美适用于您自己或任何 第三方组件,只要它们附加传递的 className prop 到 DOM 元素。

import React from 'react';
import styled from 'styled-components';
// import Button from 'react-bootstrap/Button';

import color from '../config/color';

const Button = ({ children, className onPress }) => (
  <button type="button" className={className} onPress={onPress}>{children}</button>
);

const StyledButton = styled(Button)`
  width: 12rem;
  height: 54px;
  font-size: 1rem;
  background-color: ${(props) => {
    if (props.inverted) return 'white';
    if (props.disabled) return color.disabled;
    return (props.color || color.primary);
  }};
  color: ${(props) => {
    if (props.disabled) return color.disabledText;
    if (props.inverted) return (props.color || color.primary);
    return 'white';
  }};
  border:${(props) => (props.inverted ? `2px solid ${props.color || color.primary}` : 'none')};
  border-radius: 60px;

  &:hover {
    filter: ${(props) => (props.inverted || props.disabled ? 'none' : 'brightness(95%)')}
  }
`;

export default StyledButton;

阅读styled-component documentation 了解更多关于任何组件样式的详细信息

【讨论】:

  • 感谢您的回答,我在阅读文档时有完全相同的想法,但我未能实现它。你能看看我更新的代码,看看哪个部分出了问题吗?因为一旦我这样实现,样式就消失了
  • @issac,您需要将 classesName 传递给您想要设置样式的 DOM 元素。根据文档:The styled method works perfectly on all of your own or any third-party component, as long as they attach the passed className prop to a DOM element.
【解决方案2】:

让我们重命名样式按钮组件,以减少两个名称相似的组件之间的混淆。

styled-button.tsx:

import styled from 'styled-components';

const StyledButton = styled.button`
  background-color: 'green'
`

export default StyledButton; 

当您将样式按钮组件导入您的 Button 组件时,您实际上可以像使用传统 HTML &lt;button&gt; 元素时通常使用的方式一样使用它,因为它的 props 被暴露并可用样式化的组件也是如此。

button.tsx:

import StyledButton from './StyledButton'

class Button extends React.Component {
  componentDidMount() {
    const { someProps, otherProps } = this.props;
    // some lifecycle logic
  }

  handleClick() {
     // do the rest
  }

  render() {
    return <StyledButton onClick={() = this.handleClick()} />;
  }
}

如果你愿意,你甚至可以将 props 从父组件 Button 传递给子组件 StyledButton。这将允许您对其进行自定义。

render() {
  const { color } = this.props;

  return <StyledButton background={color} onClick={() = this.handleClick()} />;
}

在您的 StyledButton 组件上,您只需要进行以下更改:

const StyledButton = styled.button`
  background-color: ${({ color }) => color || 'green'}
`

【讨论】:

    【解决方案3】:

    对于自定义组件的样式,例如Button,您必须通过 className 属性来设置其他答案。

    样式是injected through className property

    const ButtonDefaultStyle = styled.button`
      width: 5rem;
    `;
    
    const Button = ({ className, children, onPress }) => (
      <ButtonDefaultStyle className={className} type="button" onPress={onPress}>
        {children}
      </ButtonDefaultStyle>
    );
    
    export default Button;
    

    然后可以应用样式:

    import Button from './Button.js'
    
    // Will override width: 5rem;
    const StyledButton = styled(Button)`
      width: 12rem;
    `;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2022-11-13
      • 1970-01-01
      • 2020-04-28
      相关资源
      最近更新 更多