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";
}

[React] Style a React component with styled-components的更多相关文章

  1. React.js 样式组件:React Style

    点这里 React Style 是 React.js 可维护的样式组件.使用 React Native StyleSheet.create一样的样式. 完全使用 JavaScript 定义样式: ? ...

  2. React.Component 与 React.PureComponent(React之性能优化)

    前言 先说说 shouldComponentUpdate 提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看 ...

  3. [React] Use React.cloneElement to Extend Functionality of Children Components

    We can utilize React.cloneElement in order to create new components with extended data or functional ...

  4. React 的 PureComponent Vs Component

    一.它们几乎完全相同,但是PureComponent通过prop和state的浅比较来实现shouldComponentUpdate,某些情况下可以用PureComponent提升性能 1.所谓浅比较 ...

  5. React.createClass和extends Component的区别

    React.createClass和extends Component的区别主要在于: 语法区别 propType 和 getDefaultProps 状态的区别 this区别 Mixins 语法区别 ...

  6. React.createClass 、React.createElement、Component

    react里面有几个需要区别开的函数 React.createClass .React.createElement.Component 首选看一下在浏览器的下面写法: <div id=" ...

  7. React Native 中的component 的生命周期

    React Native中的component跟Android中的activity,fragment等一样,存在生命周期,下面先给出component的生命周期图 getDefaultProps ob ...

  8. React源码 React.Component

    React中最重要的就是组件,写的更多的组件都是继承至 React.Component .大部分同学可能都会认为 Component 这个base class 给我们提供了各种各样的功能.他帮助我们去 ...

  9. React.Component 和 React.PureComponent 、React.memo 的区别

    一 结论 React.Component 是没有做任何渲染优化的,但凡调用this.setState 就会执行render的刷新操作. React.PureComponent 是继承自Componen ...

随机推荐

  1. 大型情感类电视连续剧--Android高德之旅(2)地图类型

    总要说两句 今天继续我们的Android高德之旅,上一篇已经能够显示最主要的地图了.有主要的放大缩小功能.还有最后做的点击3D旋转.倾斜视角的效果.今天这篇文章来记录一下高德地图的5种地图类型. (其 ...

  2. js插件---10个免费开源的JS音乐播放器插件

    js插件---10个免费开源的JS音乐播放器插件 一.总结 一句话总结:各种插件都有很多,多去找. 二.js插件---10个免费开源的JS音乐播放器插件 亲测可用 音乐播放器在网页设计中有时候会用到, ...

  3. Zabbix + Grafana

    Grafana 简介 Grafana自身并不存储数据,数据从其它地方获取.需要配置数据源 Grafana支持从Zabbix中获取数据 Grafana优化了图形的展现,可以用来做监控大屏 Grafana ...

  4. sql server 内置MD5加密函数

    http://blog.csdn.net/rookie_liu_ToFly/article/details/53116932 select right(sys.fn_VarBinToHexStr(HA ...

  5. ErrorSet

    1.获取路径的失误: 例子是对一个列表项的悬浮操作: ~(function() { var lists = $(".footer_log>li"); lists.each(f ...

  6. netstat---显示Linux中网络系统的状态信息

    netstat命令用来打印Linux中网络系统的状态信息,可让你得知整个Linux系统的网络情况. 语法 netstat(选项) 选项 -a或--all:显示所有连线中的Socket: -A<网 ...

  7. 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) D】Bash and a Tough Math Puzzle

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 对于1操作 令len = r-l+1 等价于查找l..r这个范围内x的倍数的个数是否大于等于len-1 也即l..r这个范围内不是x ...

  8. DOM和SAX是应用中操纵XML文档的差别

    查看原文:http://www.ibloger.net/article/205.html DOM和SAX是应用中操纵XML文档的两种主要API.它们分别解释例如以下:          DOM.即Do ...

  9. HDU4630-No Pain No Game(离线,线段树)

    Problem Description Life is a game,and you lose it,so you suicide. But you can not kill yourself bef ...

  10. vim学习3

    可视模式: