[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) ...
随机推荐
- SVN设置钩子文件限制提交文件时必须填写更新日志
进入相应SVN仓库hooks目录,编辑文件pre-commit #!/bin/sh # PRE-COMMIT HOOK## The pre-commit hook is invoked before ...
- 关于模板中的动态取值 ---反射与javascript脚本编译
在项目中经常遇到一个问题,打印word或者打印excel的时候,我们经常使用一对一的赋值或者批量替换的方式来对模板进行修改. 但是现在遇到两种场景: 1.取值是通过自定以方法进行取值的. 如:一个销售 ...
- weka打开提示内存不足的解决方法
今天在linux中打开Weka时,打开基因数据文件的时候出现如 Not enough memory . Please load a smaller dataset or use a larger he ...
- jQuery图片滑动
一个非常简单实用的jQuery插件 可以用在页面的顶部广告展示 http://slidesjs.com/ 一个需要注意的问题, 就是在手机等客户端(IOS8以上), 使用此插件时, 经常会触发插件的r ...
- Git对于单个文件的分批提交方式的使用
很多时候,对于一个大的文件,可能有的同学改完之后不想一次提交,想分批提交.但这个时候由于git add的机制往往add之后就是整个一个文件被放到stage区了,这个时候肯定会想能不能对一个文件可以进行 ...
- Linux系统中,main函数的执行过程
http://blog.csdn.net/rrerre/article/details/6728431
- jQuery简单导航示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Python几种创建list的方法的效率对比
我们用 生成一个0到((1万倍n)-1)的list做例子 首先这种方式复杂度为平方级 ''' def test1(n): lst = [] for i in range(n*10000): lst = ...
- UVA 1594 Ducci Sequence(两极问题)
Ducci Sequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu D ...
- How Many Points of Intersection?
uva10790:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...