[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:;
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的更多相关文章
- React.js 样式组件:React Style
点这里 React Style 是 React.js 可维护的样式组件.使用 React Native StyleSheet.create一样的样式. 完全使用 JavaScript 定义样式: ? ...
- React.Component 与 React.PureComponent(React之性能优化)
前言 先说说 shouldComponentUpdate 提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看 ...
- [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 ...
- React 的 PureComponent Vs Component
一.它们几乎完全相同,但是PureComponent通过prop和state的浅比较来实现shouldComponentUpdate,某些情况下可以用PureComponent提升性能 1.所谓浅比较 ...
- React.createClass和extends Component的区别
React.createClass和extends Component的区别主要在于: 语法区别 propType 和 getDefaultProps 状态的区别 this区别 Mixins 语法区别 ...
- React.createClass 、React.createElement、Component
react里面有几个需要区别开的函数 React.createClass .React.createElement.Component 首选看一下在浏览器的下面写法: <div id=" ...
- React Native 中的component 的生命周期
React Native中的component跟Android中的activity,fragment等一样,存在生命周期,下面先给出component的生命周期图 getDefaultProps ob ...
- React源码 React.Component
React中最重要的就是组件,写的更多的组件都是继承至 React.Component .大部分同学可能都会认为 Component 这个base class 给我们提供了各种各样的功能.他帮助我们去 ...
- React.Component 和 React.PureComponent 、React.memo 的区别
一 结论 React.Component 是没有做任何渲染优化的,但凡调用this.setState 就会执行render的刷新操作. React.PureComponent 是继承自Componen ...
随机推荐
- NStimer 被堵塞
我们在界面上滚动一个scrollview,那么我们会发如今停止滚动前,会发现NSTimer未被运行.就好像scrollView在滚动的时候将timer暂停了一样,在查看对应文档后发现,这事实上就是ru ...
- 用MediaRecorder实现简单的录像功能
思路:定义一个SurfaceView用来显示预览,在SurfaceHolder的回调中用Camera对象启动预览.然后调用MediaRecorder来录像.仅仅是实现了简单的录像開始和停止功能.顶部能 ...
- OSX: 禁止iCloud钥匙链?
自从10.9有了一个新的功能叫viewlocale=zh_CN">iCloud钥匙串的,就出现了不少的麻烦.一是在10.9.3之前.好多人出现无限循环地要求用户输入Local item ...
- Android 学习笔记进阶十二之裁截图片
package xiaosi.cut; import java.io.File; import android.app.Activity; import android.content.Intent; ...
- css中linear-gradient的使用
注明:此篇文章来自http://www.cnblogs.com/lhb25/archive/2013/01/30/css3-text-shadow.html.
- 用AOP改善javascript代码
Aop又叫面向切面编程,用过spring的同学肯定对它非常熟悉,而在js中,AOP是一个被严重忽视的技术点,这篇就通过下面这几个小例子,来说说AOP在js中的妙用. 1, 防止window.onloa ...
- POJ 2981 Strange Way to Express Integers 模线性方程组
http://poj.org/problem?id=2891 结果看了半天还是没懂那个模的含义...懂了我再补充... 其他的思路都在注释里 /********************* Templa ...
- 洛谷——P1314 聪明的质监员
https://www.luogu.org/problem/show?pid=1314 题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每 ...
- iOS Dev (51)加急审核
https://developer.apple.com/appstore/contact/? topic=expedite
- [NowCoder]牛客OI周赛1 题解
A.分组 首先,认识的人不超过3个,因此不存在无解的方案 考虑直接构造,先把所有点设为1,顺序扫一遍把有问题的点加入队列 每次取队头,将其颜色取反,再更新有问题的点 复杂度:考虑到每个点不会操作2次, ...