In this lesson, we extend the styles of a base button component to create multiple variations of buttons, using "extend". We can then modify the base styles in one place, and have all button types updated.

import React from "react";
import styled from "styled-components"; const Button = styled.button`
background: ${props => props.theme.base};
color: white;
font-size: 1rem;
padding: .75rem 2rem;
box-shadow: 3px 5px rgba(, , , 0.1);
cursor: pointer;
border: none;
border-radius: 4px;
margin: .5rem;
transition: all .1s;
&:hover {
transform: translateY(1px);
box-shadow: 2px 3px rgba(, , , 0.15);
}
`; const ButtonDanger = Button.extend`background: ${props => props.theme.danger};`;
const ButtonGradient = Button.extend`
background: ${props => props.theme.gradient};
`; /* ============================== */
/* ===== the main component ===== */
/* ============================== */
const App = () =>
<div>
<Button>Basic button</Button>
<ButtonDanger>Watch out now!</ButtonDanger>
<ButtonGradient>Gradient Button!</ButtonGradient>
</div>; export default App;

theme:

import React from "react";
import ReactDOM from "react-dom";
import App from "./App"; import { ThemeProvider, injectGlobal } from "styled-components"; injectGlobal`
body {
margin: ;
padding: 2rem;
font-family: -apple-system,
BlinkMacSystemFont,
"Segoe UI",
Roboto,
Helvetica,
Arial,
sans-serif,
"Apple Color Emoji",
"Segoe UI Emoji",
"Segoe UI Symbol";
}
`; const theme = {
base: "#a04ed9",
danger: "tomato",
gradient: `background-color: #00DBDE; background-image: linear-gradient(225deg, #00DBDE %, #FC00FF %);`
}; ReactDOM.render(
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>,
document.getElementById("root")
);

[React] Create component variations in React with styled-components and "extend"的更多相关文章

  1. [React] Create & Deploy a Universal React App using Zeit Next

    In this lesson, we'll use next to create a universal React application with no configuration. We'll ...

  2. [React] Update Component State in React With Ramda Lenses

    In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. ...

  3. [React] Preventing extra re-rendering with function component by using React.memo and useCallback

    Got the idea form this lesson. Not sure whether it is the ncessary, no othere better way to handle i ...

  4. 利用 Create React Native App 快速创建 React Native 应用

    本文介绍的 Create-React-Native-App 是非常 Awesome 的工具,而其背后的 Expo 整个平台也让笔者感觉非常的不错.笔者目前公司是采用 APICloud 进行移动应用开发 ...

  5. Ch03 React/JSX/Component 簡介

    Facebook 本身有提供 Test Utilities,但由于不够好用,所以目前主流开发社群比较倾向使用 Airbnb 团队开发的 enzyme,其可以与市面上常见的测试工具(Mocha.Karm ...

  6. A Bite Of React(2) Component, Props and State

    component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome ...

  7. react slot component with args

    react slot component with args how to pass args to react props child component https://codesandbox.i ...

  8. react hooks & component will unmount & useEffect & clear up

    react hooks & component will unmount & useEffect & clear up useEffect & return === u ...

  9. [React] Create a Persistent Reference to a Value Using React useRef Hook

    The useRef is a hook for creating values that persist across renders. In this lesson we'll learn how ...

随机推荐

  1. 脚本实现自动化安装lamp&lnmp

    #备注:前提是将lnmp和lnmp自动化脚本写好放在相应的路径, 脚本已写好,请查看我博客中的 shell脚本 专栏! #!/bin/bash #安装lamp或者lnmp path=/server/s ...

  2. 【Codeforces Round #460 (Div. 2) B】 Perfect Number

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 直接暴力求出第k个perfect数字就好. 纯模拟. [代码] #include <bits/stdc++.h> #de ...

  3. 【Henu ACM Round #13 F】Fibonacci-ish

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举序列的头两个数字是什么 O(N^2) 然后头两个数字确定之后. f[3],f[4]..就确定了 只需查看f[3],f[4]..是 ...

  4. Mysql学习总结(19)——Mysql无法创建外键的原因

    在MySQL中创建外键时,经常会遇到问题而失败,这是因为Mysql中还有很多细节需要我们去留意,我自己总结并查阅资料后列出了以下几种常见原因. 1.  两个字段的类型或者大小不严格匹配.例如,如果一个 ...

  5. ItelliJ IDEA开发工具使用—创建一个web项目(转)

    最近想用IDEA编辑器开发,但是平时都用MyEclipse和eclipse习惯了,突然间用IDEA到处碰壁的感觉.在不断的摸索之后终于苦尽甘来,学会了基本的web程序如何创建以及运行了.期间在网上找了 ...

  6. vue.js 第一课:实例化vue

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. HDU——T 1166 敌兵布阵

    http://acm.hdu.edu.cn/showproblem.php?pid=1166 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  8. Spring : 征服数据库(一)

    严格的说.这里征服的是关系型数据库.之后笔者会以MongoDB为例,给出非关系型数据库的解决方式,敬请期待. 获取连接,操作,关闭,不知所云的异常...是的,你受够了.在使用纯JDBC时你訪问数据库时 ...

  9. linux vim,vi编辑器的基础

    在之前我也不知道为什么要学vim和vi编辑器,仅仅是听过别人说这个对与linux非常重要. 先说一下为什么要学习这个编辑器.个人的看书理解. 系统管理员的重要工作就是參与与设置某些重要软件的配置文件, ...

  10. 关于mysql数据库在输入password后,滴的一声直接退出界面的解决的方法(具体办法)

    前一阵子.因为敲代码要用到数据库,便在本子上下载了一个,却出现非常多小问题(自己的台式机却没有该问题,可能是本人的本子太渣了吧),纠结了好一阵,回头想想.发现问题,分析问题,解决这个问题,不就是我们的 ...