[React] Style a React component with styled-components
2017-08-02 02:38
337 查看
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:
Convert to styled-complement:
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"; }
相关文章推荐
- [React] Style the body element with styled-components and "injectGlobal"
- [React] Create component variations in React with styled-components and "extend"
- [React] Theme your application with styled-components and "ThemeProvider"
- [React] Animate your user interface in React with styled-components and "keyframes"
- 试着用React写项目-利用styled-components解决样式问题
- ReactJS component names must begin with capital letters?!
- [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component
- [Angular] Dynamic components with ComponentFactoryResolver
- [React] Refactor a Stateful List Component to a Functional Component with React PowerPlug
- [Recompose] Stream a React Component from an Ajax Request with RxJS
- [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream
- Understanding the Flex 4 Spark Component Architecture and how to Build Custom Components with the Flex 4 SDK
- [React] Styling React Components With Aphrodite
- react native ios添加ART.xcodeproj(ReactNative No component found for view with name “ARTSurfaceView”)
- [React] Make Controlled React Components with Control Props
- The CORBA Component Model: Part 2, Defining Components with the IDL 3.x Types
- [React] Recompose: Theme React Components Live with Context
- [React] Update Component State in React With Ramda Lenses
- React:styled-components
- CListCtrlStyled(可以为每一个cell定义字体和颜色的CListCtrl) Creating a CListCtrl Class with Item Style Features (CListCtrlStyled Class)