[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) ...
随机推荐
- json+一般处理程序读取数据库数据
一般处理程序的语法结构 string jsoncallback = context.Request["jsoncallback"]; 声明变量 前台传值使用 stri ...
- UITextField AutoComplete iOS输入框内文本自动完成
当你打开Safari的时候,输入网址,会有许多候选网址,点击后,自动填充到输入框,进入网页. 打开词典查单词的时候,输入前面部分字母,软件会给出符合的候选单词. 这样做的目的,是为了省去用户繁琐的输入 ...
- gearmand的安装
1.安装gperf libuuid-devel yum install -y gperf libuuid-devel 2.安装 libevent yum install libevent libeve ...
- 理解O/R Mapping
本文的目的是以最精炼的语言,理解什么是O/R Mapping,为什么要O/R Mapping,和如何进行O/R Mapping. 什么是O/R Mapping? 广义上,ORM指的是面向对象的对象模型 ...
- 【jquery学习笔记】关于$(window),$("html,body").scroll()的在不同浏览器的不同反应
已经很几次碰到了这种问题, 例子: $(window).scroll(function(){ var num=$(window).scrollTop(); //之前的写法是$ ...
- [标签] action的使用
1.描述 This tag enables developers to call actions directly from a JSP page by specifying the action n ...
- Day3 set集合、函数和装饰器
set特性:无序,不重复,可嵌套 创建setset_example = set("123", "213", "234", "432 ...
- python使用PIL压缩图片
import Image import os import os.path import sys path = sys.argv[1] small_path = (path[:-1] if path[ ...
- function的粗浅理解
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- native2ascii 在 Mac终端的转码
打开终端,输入 native2ascii 回车 然后输入想要转码的 中文 回车 自动会出现 通用的 unicode编码. 默认应该是 utf-8 格式