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. int long long 的取值范围

    unsigned   int   0-4294967295   //整型的每一种都有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明 ...

  2. Java Security安全系列文档翻译笔记————KeyStore、密钥、证书、命令行实战

    发送方任务: 1.将文档.源代码打包到jar包(这样才干够签名) 2.在keystore中生成相应的Private key和Public key 3.用Private Key对jar包进行签名,这是j ...

  3. myeclipse中断点调试

    在代码最左端,也就是行号位置处双击.会出现一个实心小圆点.即增加的断点.debug启动程序,就会运行到断点处: 按F5是进去方法里面. 按F6是一步一步走, 按F7是跳出方法里面(按F5后再按F7就跳 ...

  4. Springboot优化

    https://www.cnblogs.com/chen110xi/p/6198481.html

  5. Python day字符串所有使用

    字符串所有的操作name = "dio"names = "my\t name is {Name} and i am a {job}"print(name.cap ...

  6. C# Unable to load DLL 'WzCanDll.dll':找不到指定的模块

    一.打开app无法加载DLL 我用C++编写的DLL,然后用C#写的界面APP,在自己的电脑上打开没有问题,放在其它电脑上就出现无法加载DLL库的问题,一连接APP就会出现问题,如下图所示: 二.解决 ...

  7. [Javascript] Classify JSON text data with machine learning in Natural

    In this lesson, we will learn how to train a Naive Bayes classifier and a Logistic Regression classi ...

  8. apache activemq Failed to bind to server socket 61616

    windows环境上: 首先 nestat -ano | findstr "61616" 查看是否有进程,有的话kill掉, 若没有发现,查看windows Internet Co ...

  9. call、apply、bind 区别

    1.为什么要用 call .apply? 为了 改变方法里面的属性而不去改变原来的方法 function fruits() {} fruits.prototype = { color: "r ...

  10. JS/CSS 在屏幕底部弹出消息

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...