styled-components 简介

2025-05-25

styled-components 简介

是的,我一直和你一样,我一直使用 classNames 来设置 React 应用中 html 元素的样式,因为那是React中唯一可用的方法。😒

完成一个项目后,回去维护或编辑最近完成的项目总是令人感到压力很大☹️。

我还学习了BEM语法。BEM 确实很有帮助,但对我来说,它对 React 来说仍然不是最好的选择。问题还没解决!😓

直到几周前,我在看一个教程视频时偶然发现了样式组件。听起来很酷,但它到底是什么?
它能解决我的问题吗?能减轻我的压力吗?
让我们来一探究竟!😊

对 Styled Components 感到好奇

那么 styled-components 到底是什么?

根据官方网站介绍,styled components 的诞生源于我们探索如何增强 CSS 来为 React 组件系统添加样式。它充分利用了 ES6 和 CSS 的精髓,让你轻松设计应用样式。🎉

那么如何在 React 应用程序中使用 styled-components 呢?

通过 NPM 安装

npm install --save styled-components
Enter fullscreen mode Exit fullscreen mode

一旦安装完成就完成了。🎉

让我们使用 styled-components 构建这个简单的设计

样式组件示例

为了在你的 React 应用中使用 styled-components,你必须导入 styled-components,像这样

import styled from "styled-components"
Enter fullscreen mode Exit fullscreen mode

现在,对于上面的例子,组件看起来是这样的

import React from "react"
import styled from "styled-components" //The styled import


//In styled-components, every element is declared as a variable 

//and you are free to use any variable name you like.

//styled.main: main represents the html tag <main></main> that's 

//what Container is representing

const Container = styled.main` 
  width: 90%;
  background-color: #fff;
  padding: 30px;
  border-radius: 15px
`

//Header represents the h2 tag
const Header = styled.h2`
color: #192041;
font-weight: 700;
margin-bottom: 15px;
font-family: "Merriweather", serif;
`

//Form represents the form tag
const Form = styled.form`
width: 100%;
margin: 15px 0px;
display: flex;
align-items: center
` 

//InputContainer represents the input tag
const InputContainer = styled.input`
flex: 0.8;
margin-right: 10px;
height: 35px;
background-color: #e6e9fb;
border: 1px solid #ddd;
outline: none;
padding: 15px
`
//SubHeader represents the p-tag 
const SubHeader = styled.p`
  color: #000;
  font-weight: 500;
  font-size: 14px
`
//What happened here is that ParagraphCommon inherits the style of

//SubHeader, just like giving them the same styles. You can also 

//add specific styles within the quotes

const ParagraphCommon = styled(SubHeader)``

const SearchButton = styled.button`
height: 35px;
background-color: #192041;
flex: 0.2;
border: none;
color: #fff`

const ButtonGroup = styled.div`
width: 100%;
margin: 15px 0px;
display: flex;
align-items: center
`
const Button = styled.button`
  margin-right: 15px;
  width: 100px;
  padding: 7px;
  border: none;
  cursor: pointer;
  border-radius: 5px;
  color: ${props => props.textColor};
  background-color: ${props => props.bgColor}

//Here, styled-components receive props from the elements in the

//components, which are then accessed using this syntax, which 

//gives you the opportunity to do conditional styling.
`

//The React component
function App() {
  return (
    <Container>
        <Header>
          Free and Pro website templates for your business
        </Header>
        <SubHeader>
          Choose from over 300 fully responsive portfolio, blog and ecommerce templates
        </SubHeader>
        <Form>
          <InputContainer placeholder="Search all templates"/>
          <SearchButton>Search</SearchButton>
        </Form>

        <ParagraphCommon>
            Common searches
        </ParagraphCommon>

        <ButtonGroup>
            <Button bgColor="#f1c0de" textColor="#F037A5">Blog</Button>
            <Button bgColor="#e0c3fa" textColor="#8C5EB8">Business</Button>
            <Button bgColor="#d0fccd" textColor="#77D970">Portfolio</Button>
            <Button bgColor="#F9E4C8" textColor="#F78812">Ecommerce</Button>
        </ButtonGroup>
    </Container>
  );
}

export default App;

Enter fullscreen mode Exit fullscreen mode

更有趣的观点:

关于 styled-components 还有很多内容,你一定会喜欢的。点击此处了解更多

欢迎在
🚀 Linkedin
🚀 Twitter上关注我并与我联系

感谢您的阅读!🎉

styled-components 教程已完成/></p>

文章来源:https://dev.to/arshadayvid/stop-using-classnames-to-style-your-react-apps-instead-use-53jn
PREV
使用 JavaScript 进行语音识别
NEXT
Javascript GameDev 生态系统概述