[React] Style a React component with styled-components
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[React] Style a React component with styled-components,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3543字,纯文字阅读大概需要6分钟。
内容图文
![[React] Style a React component with styled-components](/upload/InfoBanner/zyjiaocheng/1239/24d4ff652be34b4585fa85311e43cc83.jpg)
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: 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"; }
原文:http://www.cnblogs.com/Answer1215/p/7271810.html
内容总结
以上是互联网集市为您收集整理的[React] Style a React component with styled-components全部内容,希望文章能够帮你解决[React] Style a React component with styled-components所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。