[React] Style a React component with styled-components

In this lesson, we remove the mapping between a React component and the styles applied to it via classnames. We write our styles directly within the component, in a real CSS syntax, with the full power of JavaScript.

 

Old:

复制代码
import React, { Component } from "react";
import "./App.css";

/* ======================= */
/* ===== sample data ===== */
/* ======================= */
const people = [
  { name: "Anna", sex: "female", age: 28 },
  { name: "John", sex: "male", age: 31 },
  { name: "Tim", sex: "male", age: 7 },
  { name: "Bella", sex: "female", age: 4 }
];

/* ============================== */
/* ===== the main component ===== */
/* ============================== */
const App = () =>
  <div>
    {people.map((person, i) => {
      return (
        <Person name={person.name} age={person.age} sex={person.sex} key={i} />
      );
    })}
  </div>;

/* =========================== */
/* ===== the Person card ===== */
/* =========================== */
const Person = props =>
  <div className={`person person--${props.sex}`}>
    <h2 className="person__name">{props.name}</h2>
    <p className="person__description">
      This <strong>{props.sex}</strong> is currently{" "}
      <strong>{props.age} years old</strong>.
    </p>
  </div>;

export default App;
复制代码
复制代码
.person {
  padding: 1.75rem;
  margin: .5rem;
  border-radius: 4px;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);
  color: white;
}

.person--male {
  background: #44bccc;
}

.person--female {
  background: #f973bc;
}

.person__name {
  margin-top: 0;
  font-weight: 900;
  margin-bottom: .75rem;
}

.person__description {
  margin: 0;
  line-height: 1.5;
}

.person__description strong {
  font-weight: 900;
}

body {
  margin: 0;
  font-family: -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    Roboto,
    Helvetica,
    Arial,
    sans-serif,
    "Apple Color Emoji",
    "Segoe UI Emoji",
    "Segoe UI Symbol";
}
复制代码

 

Convert to styled-complement:

复制代码
import React, { Component } from "react";
import styled from "styled-components";
import "./App.css";

/* ======================= */
/* ===== sample data ===== */
/* ======================= */
const people = [
  { name: "Anna", sex: "female", age: 28 },
  { name: "John", sex: "male", age: 31 },
  { name: "Tim", sex: "male", age: 7 },
  { name: "Bella", sex: "female", age: 4 }
];

/* ============================== */
/* ===== the main component ===== */
/* ============================== */
const App = () =>
  <div>
    {people.map((person, i) => {
      return (
        <Person name={person.name} age={person.age} sex={person.sex} key={i} />
      );
    })}
  </div>;

const Name = styled.h2`
  margin-top: 0;
  font-weight: 900;
  margin-bottom: .75rem;
`;

const Bio = styled.p`
  margin: 0;
  line-height: 1.5;
  strong {
    font-weight: 900;
  }
`;

const Card = styled.div`
  padding: 1.75rem;
  margin: .5rem;
  border-radius: 4px;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);
  color: white;
  background: ${props => (props.sex === "male" ? "#44bccc" : "#f973bc")}
`;

/* =========================== */
/* ===== the Person card ===== */
/* =========================== */
const Person = props =>
  <Card sex={props.sex}>
    <Name>{props.name}</Name>
    <Bio>
      This <strong>{props.sex}</strong> is currently{" "}
      <strong>{props.age} years old</strong>.
    </Bio>
  </Card>;

export default App;
复制代码
复制代码
body {
  margin: 0;
  font-family: -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    Roboto,
    Helvetica,
    Arial,
    sans-serif,
    "Apple Color Emoji",
    "Segoe UI Emoji",
    "Segoe UI Symbol";
}
复制代码

 

posted @   Zhentiw  阅读(619)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-08-02 [React] Set up React apps with zero configuration
2014-08-02 [Backbone]6. Collections.
2014-08-02 Immediately-Invoked Puzzler
2014-08-02 [Javascipt] Immediately-Invoker 2
2014-08-02 [Javascirpt] Immediately-Invoked function!!! IMPORTANT
2014-08-02 [Javascript] Using map() function instead of for loop
2014-08-02 [Javascript] Function Expression Ex, Changing Declarations to Expressions
点击右上角即可分享
微信分享提示