[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设备 cocos2dx 骨骼动画注册事件播放音效,退到后台再返回黑屏问题
最近遇到一个cocos2dx 骨骼动画注册事件播放音效,在骨骼动画播放的时候,按HOME键退到桌面,再次打开游戏的时候,会黑屏. 解决办法如下,可能不是太完美,至少解决了大部分问题. 1.在org.c ...
- ASP.NET菜鸟之路之登录系统
背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. 网站介绍 根据书上的例子做了一个比较粗糙的登录例 ...
- hibernate_validator_07
一.校验组序列 默认情况下,约束的验证是没有一定的顺序的,不管他们是属于哪个认证组的.但是在有些环境中,我们控制这些约束验证的顺序还是很有用的. 就拿我们上一个例子来说,我们可以这样:首先在我们检查车 ...
- 不同浏览器对parseInt方法解析的不同
parseInt方法的作用是将字符串转换为数字 当parseInt解析的时候只有0x和非0开头的数字,浏览器解析都一致,例如”0xA1”或 “9”. 只有当开头为0的时候才出现不同.IE,chrome ...
- margin-top在IE与其他浏览器下的不同
两个box,box1嵌套box2, box2使用margin-top在IE下与其他浏览器不同.待整理
- 『重构--改善既有代码的设计』读书笔记----Split Temporary Variable
继续开始我们重构手法的系列,今天介绍的是Split Temporary Variable---分解临时变量. 在我们平常写的程序中肯定有某些临时变量被赋予了超过一个的责任.如果他们不是那种收集结果(t ...
- 浮点与整形在GUI下的相关思考
平时不接触绘图,似乎感觉不到浮点和整形所带来的区别,这次项目中意外的碰到了浮点与整形进行迁移的工作.因此写点心得,让自己以后也可以看看. 用浮点作图有个最大的弊端就是边界情况,比如你需要在点(20,2 ...
- Dede 查询附加表
<!--使用dede:arclist取出信息 dede_archives 表 $sql="Select 字段 from dede_archivies",但是在默认情况下 de ...
- thinkphp T方法
为了更方便的输出模板文件,新版封装了一个T函数用于生成模板文件名. 用法: T([资源://][模块@][主题/][控制器/]操作,[视图分层]) T函数的返回值是一个完整的模板文件名,可以直接用于d ...
- JDBC与javaBean
1.JDBC的概念: Java数据库连接技术(Java DataBase Connectivity)能实现java程序对各种数据库的访问, 由一组使用java语言编写的类 和 接口(jdbc api) ...