简体   繁体   English

如何在我的反应应用程序中使用样式化组件?

[英]How to work with styled components in my react app?

I had trouble naming this question and it seems quite broad, so, forgive me oh moderators. 我无法命名这个问题而且看起来相当广泛,所以,原谅我哦主持人。 I'm trying out styled components for the first time and trying to integrate it into my react app. 我第一次尝试使用样式化组件并尝试将其集成到我的反应应用程序中。 I have the following so far: 到目前为止,我有以下内容:

import React from 'react';
import styled from 'styled-components';

const Heading = styled.h1`
    background: red;
`;

class Heading extends React.Component {

    render () {

        return (
            <Heading>
                {this.props.title}
            </Heading>
        );
    }

}

export default Heading;

So, just a normal class, but then I import styled components up top, define the const Heading , where I specify that a Heading really is just a styled h1 . 所以,只是一个普通的类,但后来我将styled components导入顶部,定义const Heading ,我指定Heading真的只是一个样式的h1 But I get an error stating that Heading is a duplicate declaration since I also say class Heading... . 但我得到一个错误,说明Heading是一个重复的声明,因为我也说class Heading...

I'm obviously completely missing something here. 我显然在这里完全遗漏了一些东西。 All the examples online doesn't actually show how you also use this with React. 在线的所有示例实际上并没有显示您如何在React中使用它。 Ie where do I define my class, my constructor, set my state, etc. 即我在哪里定义我的类,我的构造函数,设置我的状态等。

Do I have to move the styled component into it's own file, ie: 我是否必须将样式化组件移动到它自己的文件中,即:

import styled from 'styled-components';

const Heading = styled.h1`
    background: red;
`;

export default Heading;

Then create a React component that will serve as a wrapper of sorts, eg 'HeadingWrapper': 然后创建一个React组件,它将作为排序的包装,例如'HeadingWrapper':

import React from 'react';
import Heading from './Heading';

class HeadingWrapper extends React.Component {

    render () {

        return (
            <Heading>
                {this.props.title}
            </Heading>
        );
    }

}

export default HeadingWrapper;

A bit of clarity on this would greatly be appreciated! 对此有点清晰将非常感激! Thanks :) 谢谢 :)

styled.h1`...` (for example) returns a React component that works just like <h1> . styled.h1`...` (例如)返回一个与<h1>类似的React组件。 In other words, you use <h1> like this: 换句话说,你使用<h1>这样:

<h1>h1's children</h1>

...so when you do const Heading = styled.h1`...`; ...所以当你做const Heading = styled.h1`...`; , you'll use <Heading> the same way: ,你将以相同的方式使用<Heading>

<Heading>Heading's children</Heading>

If you want a component that behaves differently, eg one that uses the title prop instead of children , you'll need to define such a component, and it will need to have a different name than the Heading component you already defined. 如果您想要一个行为不同的组件,例如使用title道具而不是children组件的组件,则需要定义这样的组件,并且它需要具有与您已定义的标题组件不同的名称。

For example: 例如:

 const styled = window.styled.default; const Heading = styled.h1` background: red; `; const TitleHeading = ({title}) => <Heading>{title}</Heading>; // ...or... class StatefulTitleHeading extends React.Component { render() { return <Heading>{this.props.title}</Heading>; } } ReactDOM.render( <div> <Heading>I'm Heading</Heading> <TitleHeading title="I'm TitleHeading"/> <StatefulTitleHeading title="I'm StatefulTitleHeading"/> </div>, document.getElementById('container') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <script src="https://unpkg.com/styled-components@1.4.3/dist/styled-components.js"></script> <div id="container"></div> 

Frankly, though, it makes more sense to just use the component returend by styled.h1 directly: 坦率地说,虽然,它更有意义只使用由组件returend styled.h1直接:

const Heading = styled.h1`...`;
export default Heading;

// ...then...

<Heading>Children go here</Heading>

The semantics of children are already clear, and using <Heading title="Children go here"/> instead detracts significantly from that. 儿童的语义已经很清楚了,使用<Heading title="Children go here"/>反而显着地减少了这一点。

Let me make this really simple for you. 让我为你做这个很简单。

Let's create one styled component for heading named 'Heading' 让我们为名为'Heading'的标题创建一个样式组件

const Heading = styled.h1`
  color: 'black';
  font-size: 2rem;
`

and now you can use it like following. 现在你可以像下面一样使用它。

<Heading>{this.props.title}</Heading>

How you manage to get the title prop as it's child is no concern of style component's. 如何获得标题道具,因为它的孩子不关心风格组件。 It only manages how that title looks. 它只管理标题的外观。 Styled component is not a container that maintains your app/business logic. 样式化组件不是维护您的应用程序/业务逻辑的容器。

Now let's look at an example in it's entirety. 现在让我们看一下整个示例。

import styled from 'styled-components'

// Heading.js (Your styled component)

const Heading = styled.h1`
  color: 'black';
  font-size: 2rem;
`
export default Heading

and now your container that will use your styled component 现在你的容器将使用你的样式组件

// Header.jsx (Your container)
class Header extends Component {

  componentWillReceiveProps(nextProps) {
    // This your title that you receive as props
    console.log(nextProps.title)
  }

  render() {
    const { title } = this.props
    return (
      <div id="wrapper">
        <Heading>{title}</Heading>
      </div>
    )
  }
}

I hope that helps. 我希望有所帮助。 Let me know if you need further clarification. 如果您需要进一步澄清,请与我们联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM