`避免样式命名空间重复的问题``

1.安装依赖

  1. yarn add styled-components

2.创建一个样式组件

  1. //Wrapper
  2. import styled from 'styled-components';
  3. const Title = styled.h1`
  4. font-size: 1.5em;
  5. text-align: center;
  6. color:#C0C342
  7. `;
  8. export default Title;

3.使用

  1. import React from 'react';
  2. import Title from './components/Wrapper';
  3. class App extends React.Component {
  4. render() {
  5. return (
  6. <div>
  7. <Title>react</Title>
  8. </div>
  9. )
  10. }
  11. }
  12. export default App;

4.嵌套

  1. import styled from 'styled-components';
  2. const Title = styled.h1`
  3. font-size: 1.5em;
  4. text-align: center;
  5. color:#C0C342;
  6. .one{
  7. color:blue;
  8. }
  9. h4{
  10. color:red;
  11. }
  12. &:hover{
  13. color:green;
  14. }
  15. `;
  16. export default Title;