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:;
font-weight:;
margin-bottom: .75rem;
} .person__description {
margin:;
line-height: 1.5;
} .person__description strong {
font-weight:;
} body {
margin:;
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:;
font-family: -apple-system,
BlinkMacSystemFont,
"Segoe UI",
Roboto,
Helvetica,
Arial,
sans-serif,
"Apple Color Emoji",
"Segoe UI Emoji",
"Segoe UI Symbol";
}

最新文章

  1. Building a RESTful Web Service
  2. CSS中如何让元素隐藏
  3. linux下python调用c模块
  4. Solve problem &#39;SURF&#39; is not a member of &#39;cv&#39;
  5. MSCRM4.0如何使js事件在批量编辑表单中触发
  6. 研究一下uucode编码
  7. 在WPF中自定义你的绘制(五)
  8. 阅读国外大神对this的分析,自己的总结
  9. hdu 3342 Legal or Not(拓扑排序)
  10. clientHeight、offsetHeight、scrollHeight详解
  11. Spring框架学习笔记(10)——Spring中的事务管理
  12. 2、MyEclipse和Eclipse调优,MyEclipse配置(tomcat和jdk的内存设置),jar引入相关知识点,将Java项目编程web项目的办法
  13. 51nod-1627 瞬间移动(组合数+逆元)
  14. 学习笔记TF067:TensorFlow Serving、Flod、计算加速,机器学习评测体系,公开数据集
  15. Servlet 使用ServletConfig对象来配置Servlet
  16. Pycharm远程连接服务器,并在本地调试服务器代码
  17. 关于metaspolit中进行JAVA反序列化渗透RMI的原理分析
  18. Xamarin Forms:小马过河,王者归来
  19. 利用raphael画图
  20. eclipse decompiler

热门文章

  1. 【剑指offer】Q25:二叉树中和为某一值的路径
  2. Viewpager切换时pager页面的生命周期变化
  3. Ajax缓存原理
  4. Android java.lang.NoSuchFieldError: No static field xxx of type I in class Lcom/XX/R$id; or its superclasses
  5. LBP 特征
  6. html页面中块级元素的居中
  7. JavaScript--数据结构与算法之散列
  8. 解决Not allowed to load local resource
  9. 01-Jvm 内存区域复习笔记
  10. [NOIP2013]车站分级 解题报告