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的更多相关文章

  1. [React] Intro to inline styles in React components

    React lets you use "inline styles" to style your components; inline styles in React are ju ...

  2. 从 0 到 1 实现 React 系列 —— 2.组件和 state|props

    看源码一个痛处是会陷进理不顺主干的困局中,本系列文章在实现一个 (x)react 的同时理顺 React 框架的主干内容(JSX/虚拟DOM/组件/生命周期/diff算法/setState/ref/. ...

  3. 学习React系列(十)——Render Props

    解决问题:将行为封装,供多个组件使用(在多个组件之间分享某段代码) 组件中的props属性中包含一个"render"属性(该属性为一个返回值为元素的方法),然后在该组件的rende ...

  4. React Native 快速入门之认识Props和State

    眼下React Native(以后简称RN)越来越火,我也要投入到学习当中.对于一个前端来说,还是有些难度.因为本人觉得这是一个App开发的领域,自然是不同.编写本文的时候,RN的版本为0.21.0. ...

  5. React高阶组件 和 Render Props

    高阶组件 本质 本质是函数,将组件作为接收参数,返回一个新的组件.HOC本身不是React API,是一种基于React组合的特而形成的设计模式. 解决的问题(作用) 一句话概括:功能的复用,减少代码 ...

  6. [React] Use Prop Collections with Render Props

    Sometimes you have common use cases that require common props to be applied to certain elements. You ...

  7. React Native 一个组件styles BUG

    'use strict'; var React = require('react-native'); var { StyleSheet, PanResponder, View, Text } = Re ...

  8. Android 更改按钮样式 Button Styles

    extends:http://stackoverflow.com/questions/26346727/android-material-design-button-styles   I will a ...

  9. react设置默认state和默认props

    1.默认状态设置 1.constructor (ES6) constructor(props) { this.state = { n: ... } } 2.getInitialState (ES5) ...

随机推荐

  1. Swift - 33 - 返回函数类型和函数嵌套

    //: Playground - noun: a place where people can play import UIKit /*---------------------------返回函数类 ...

  2. oracle查询表信息(索引,外键,列等)

    oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息,索引信息查询SQL如下,希望对大家有所帮助:1.查询出所有的用户表 select * from user_tables ...

  3. JS屏蔽右键菜单,复制,粘帖xxxxx........

    //屏蔽右键菜单 document.oncontextmenu = function (event) { if (window.event) { event = window.event; } try ...

  4. cocos2dx 资源合并.

    文件合并之前 文件合并之后 吐槽 我们项目比较奇葩, ui用cocostudio做, 这项光荣的任务由美术接手. 这个美术是个新手, 经过我长时间的观察, 她似乎不用怎么画画. 至少在很长一段时间里, ...

  5. 破解Inode客户端使用笔记本共享WIFI

    由于住在学校的公寓里面,所以使用的是校园网,但是校园网限制了无线的使用,强制所有网络用户使用INode网络客户端,这个客户端不但很丑很难看,而且每天联网十分费劲,费了半天的力气终于联上网了,可是一眨眼 ...

  6. js 刷新页面大全

    一.先来看一个简单的例子: 下面以三个页面分别命名为frame.html.top.html.bottom.html为例来具体说明如何做. frame.html 由上(top.html)下(bottom ...

  7. Flask学习记录之使用Werkzeug散列密码

    数据库中直接存放明文密码是很危险的,Werkzeug库中的security能够方便的实现散列密码的计算 security库中 generate_password_hash(password,metho ...

  8. ie6789和其他浏览器之间的鼠标左、中、右键的event.button不一致的办法

      左键 中键 右键 Ie6 1 4 2 Ie7 1 4 2 Ie8 1 4 2 Ie9和其它 0 1 2 以下代码将IE6/7/8的值转换成符合W3C标准的方法: var ie678 = !-[1, ...

  9. Pycharm常用快捷键(后期慢慢补充)

    用到一个,就补充一个,慢慢来,找到自己常用的快捷键. CTRL /: 注释.取消注释行 CTRL Q: 在参数列表位置,显示可以输入的所有参数. #查看参数的详细信息

  10. bzoj2071: [POI2004]JAS

    Description 在Byteotia有一个洞穴. 它包含n 个洞室和一些隧道连接他们. 每个洞室之间只有一条唯一的路径连接他们. Hansel 在其中一个洞室藏了宝藏, 但是它不会说出它在哪. ...