[React] Radium: Updating Button Styles via Props
In a CSS library like Bootstrap we can set a button's style to be "primary" or "secondary" by appending classes. For React components we want to be able to do this via props. Radium enables this by composing styles via an array. This mimicks the cascade of CSS.
Radiumn allows 'style' attr accepts array. Inside array, the larger index style will overwrite the pervious index's style.
<button style={[
styles.base,
type==='primary' && styles.primary
]}>
{children}
</button>
So in the code, styles.primary will overwrite the styles.base:
const styles = {
base: {
backgroundColor: '#aaa',
border: 'none',
borderRadius: 4,
color: '#fff',
padding: '5px 20px',
':hover': {
backgroundColor: '#08f'
}
},
primary: {
backgroundColor: '#07d'
}
}
We can pass a props to the component to tell when should apply styles.primary style:
const { render } = ReactDOM
const rootEl = document.getElementById('root')
const Button = Radium(({ children, kind }) => (
<button style={[
styles.base,
kind === 'primary' && styles.primary
]}>
{children}
</button>
))
const styles = {
base: {
backgroundColor: '#aaa',
border: 'none',
borderRadius: 4,
color: '#fff',
padding: '5px 20px',
':hover': {
backgroundColor: '#08f'
}
},
primary: {
backgroundColor: '#07d'
}
}
render(
<Button kind="primary">
OK
</Button>,
rootEl)
[React] Radium: Updating Button Styles via Props的更多相关文章
- [React] Intro to inline styles in React components
React lets you use "inline styles" to style your components; inline styles in React are ju ...
- 从 0 到 1 实现 React 系列 —— 2.组件和 state|props
看源码一个痛处是会陷进理不顺主干的困局中,本系列文章在实现一个 (x)react 的同时理顺 React 框架的主干内容(JSX/虚拟DOM/组件/生命周期/diff算法/setState/ref/. ...
- 学习React系列(十)——Render Props
解决问题:将行为封装,供多个组件使用(在多个组件之间分享某段代码) 组件中的props属性中包含一个"render"属性(该属性为一个返回值为元素的方法),然后在该组件的rende ...
- React Native 快速入门之认识Props和State
眼下React Native(以后简称RN)越来越火,我也要投入到学习当中.对于一个前端来说,还是有些难度.因为本人觉得这是一个App开发的领域,自然是不同.编写本文的时候,RN的版本为0.21.0. ...
- React高阶组件 和 Render Props
高阶组件 本质 本质是函数,将组件作为接收参数,返回一个新的组件.HOC本身不是React API,是一种基于React组合的特而形成的设计模式. 解决的问题(作用) 一句话概括:功能的复用,减少代码 ...
- [React] Use Prop Collections with Render Props
Sometimes you have common use cases that require common props to be applied to certain elements. You ...
- React Native 一个组件styles BUG
'use strict'; var React = require('react-native'); var { StyleSheet, PanResponder, View, Text } = Re ...
- Android 更改按钮样式 Button Styles
extends:http://stackoverflow.com/questions/26346727/android-material-design-button-styles I will a ...
- react设置默认state和默认props
1.默认状态设置 1.constructor (ES6) constructor(props) { this.state = { n: ... } } 2.getInitialState (ES5) ...
随机推荐
- Android - Layout时发生'Unfortunately xxx has stopped'
概述 我在进行LinearLayout和TableLayout的嵌套布局的时候,发生题的错误.如下布局xml代码: <LinearLayout xmlns:android="http: ...
- javadoc in archive eclipse.
Open Windows -> Preferences. 2.Select jre from Installed JREs then hit Edit botton on the right. ...
- Swift - 28 - 内部参数名和外部参数名
//: Playground - noun: a place where people can play import UIKit // 外部参数的作用是为了让程序员调用代码的时候能清晰的看出所传参数 ...
- javascript之typeof、constructor、instanceof
ref: http://jingyan.baidu.com/article/29697b912f9939ab20de3c8c.html
- c#结构体和字节数组的转换、字节数组和stream的转换
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/streambytsstruct.html using System; using ...
- python密码处理(可用于生产模式)
import os from hashlib import sha256 from hmac import HMAC def encrypt_password(password, salt=None) ...
- MongoDB学习--高级查询 [聚合Group]
Group大约需要一下几个参数. key:用来分组文档的字段.和keyf两者必须有一个 keyf:可以接受一个javascript函数.用来动态的确定分组文档的字段.和key两者必须有一个 initi ...
- jquery 选项卡实现
HTML文件 $(function(){ var $div_li =$("div.tab_menu ul li"); $div_li.click(function(){ $(thi ...
- jquery hide() show()
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 如何把PPT变小|PowerPoint文档减肥的几种方法
使用powerpoint制作幻灯片的过程中,经常出现过这样的情况,制作幻灯片时,出于内容的需要和美观的需要,添加了许多图片和Excel表或者OLE对象,成倍增大了文档的体积,结果导致: 1.页面编辑人 ...