[React] Intro to inline styles in React components
React lets you use "inline styles" to style your components; inline styles in React are just JavaScript objects that you can render in an element's style
attribute. The properties of these style objects are just like the CSS property, but they are camel case (borderRadius
) instead of kebab-case (border-radius
). React inline styles allow you to use all the JavaScript you know and love like variables, loops, ES6 modules etc. with your styles. React then renders these properties as inline styles in the output HTML, which means that styles are scoped to the component itself - no more cascading issues or trying to figure out how to re-use a component...everything that belongs to a component is self contained with the component!
/*main.js*/
import React from 'react';
import Example from './example'; React.render(<Example content="Button"/>, document.body);
/*example.js*/ import React, {Component} from 'react';
import styles from './styles/styles'; class Example extends Component {
render() {
return (
<p style={styles}>{this.props.content} Hello</p>
);
}
} Example.propTypes = {
content: React.PropTypes.string.isRequired
}; export default Example;
styles.js
const baseColor = '#4D54D8';
const borderColor = "#080E73";
const fontColor = "white"; var styles = {
color: `${fontColor}`,
background: `${baseColor}`,
borderRadius: '1rem',
border: `1px solid ${borderColor}`,
width: '150px',
height: '30px',
fontSize: '1rem',
textAlign: 'center'
}; export default styles;
[React] Intro to inline styles in React components的更多相关文章
- React Native 一个组件styles BUG
'use strict'; var React = require('react-native'); var { StyleSheet, PanResponder, View, Text } = Re ...
- React基础篇 (1)-- render&components
render 基础用法 //1.创建虚拟DOM元素对象 var vDom=<h1>hello wold!</h1> //2.渲染 ReactDOM.render(vDom,do ...
- [React] Use React.cloneElement to Extend Functionality of Children Components
We can utilize React.cloneElement in order to create new components with extended data or functional ...
- react.js 从零开始(七)React (虚拟)DOM
React 元素 React 中最主要的类型就是 ReactElement.它有四个属性:type,props,key 和ref.它没有方法,并且原型上什么都没有. 可以通过 React.create ...
- 如何使用TDD和React Testing Library构建健壮的React应用程序
如何使用TDD和React Testing Library构建健壮的React应用程序 当我开始学习React时,我努力的一件事就是以一种既有用又直观的方式来测试我的web应用程序. 每次我想测试它时 ...
- React学习笔记-1-什么是react,react环境搭建以及第一个react实例
什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...
- Override Inline Styles with CSS
inline style: <div style="background: red;"> The inline styles for this div should m ...
- React系列(一):React入门
React简介 1.由来 React是有Facebook开发出来用于构建前端界面的JS组件库,由于其背后的强大背景,使得这款库在技术开发上完全没有问题. 2.React的优势 解决大规模项目开发中数据 ...
- React Native是一套使用 React 构建 Native app 的编程框架
React Native是一套使用 React 构建 Native app 的编程框架 React Native at first sight what is React Native? 跟据官方的描 ...
随机推荐
- 帝国cms 灵动标签调用顶级栏目导航
[e:loop={"select classname,classpath from [!db.pre!]enewsclass where bclassid=0 order by classi ...
- YARN应用程序开发流程(类似于MapReduce On Yarn)本内容版权归(小象学院所有)
MapReduce On Yarn和MapReduce程序区别 MapReduce On Yarn(由专业人员开发)1 为MapReduce作业运行在YARN上提供一个通用的运行时环境2 需要与Yar ...
- [BZOJ 1014] [JSOI2008] 火星人prefix 【Splay + Hash】
题目链接:BZOJ - 1014 题目分析 求两个串的 LCP ,一种常见的方法就是 二分+Hash,对于一个二分的长度 l,如果两个串的长度为 l 的前缀的Hash相等,就认为他们相等. 这里有修改 ...
- js中的变量提升
var v='Hello World'; (function(){ alert(v); var v='I love you'; })() 会出现alert出来的是undefined,原因是因为在函数域 ...
- 关于谷歌android 4.3 ble问题
随着BLE的逐渐壮大,越来越多的厂商开始支持BLE,在未来的一年里应该会慢慢普及,谷歌android4.3已经支持了自己的nexus4和uexus7 2013手机了,也就是说如果厂商采用谷歌已经实现d ...
- R语言 多元线性回归分析
#线性模型中有关函数#基本函数 a<-lm(模型公式,数据源) #anova(a)计算方差分析表#coef(a)提取模型系数#devinace(a)计算残差平方和#formula(a)提取模型公 ...
- Linux学习笔记17——输入输出重定向
1 何谓输入输出重定向? 默认情况下输入是由键盘输入的.输出是默认的输出到屏幕上.而输入输出重定向就是改变默认的输入输出方向 2 freopen函数 freopen是被包含与stdio.h头文件中,是 ...
- UVa816 Abbott's Revenge
Abbott's Revenge Time limit: 3.000 seconds Abbott’s Revenge Abbott’s Revenge The 1999 World FinalsC ...
- Alfred(未完待续)
之前曾经有写过一篇QuickSilver的博文,大力称赞它是Mac下的神器,但是现在,QS光荣下岗了,因为我找到了另外一款比QS更加好用,更加神器的APP:Alfred小帽子. 软件名:Alfred ...
- Debian添加软件源
安装完渗透测试系统kali linux后,默认的只有security这个源,只更新那些集成的安全软件,不能安装其他新软件,官网给出了3类源: Kali Linux提供了3类软件源,这些源在世界各地都有 ...