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

  1. React Native 一个组件styles BUG

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

  2. React基础篇 (1)-- render&components

    render 基础用法 //1.创建虚拟DOM元素对象 var vDom=<h1>hello wold!</h1> //2.渲染 ReactDOM.render(vDom,do ...

  3. [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 ...

  4. react.js 从零开始(七)React (虚拟)DOM

    React 元素 React 中最主要的类型就是 ReactElement.它有四个属性:type,props,key 和ref.它没有方法,并且原型上什么都没有. 可以通过 React.create ...

  5. 如何使用TDD和React Testing Library构建健壮的React应用程序

    如何使用TDD和React Testing Library构建健壮的React应用程序 当我开始学习React时,我努力的一件事就是以一种既有用又直观的方式来测试我的web应用程序. 每次我想测试它时 ...

  6. React学习笔记-1-什么是react,react环境搭建以及第一个react实例

    什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...

  7. Override Inline Styles with CSS

    inline style: <div style="background: red;"> The inline styles for this div should m ...

  8. React系列(一):React入门

    React简介 1.由来 React是有Facebook开发出来用于构建前端界面的JS组件库,由于其背后的强大背景,使得这款库在技术开发上完全没有问题. 2.React的优势 解决大规模项目开发中数据 ...

  9. React Native是一套使用 React 构建 Native app 的编程框架

    React Native是一套使用 React 构建 Native app 的编程框架 React Native at first sight what is React Native? 跟据官方的描 ...

随机推荐

  1. JavaScript学习心得(十)

    Ajax Ajax是浏览器中使用JavaScript进行服务器后台请求,读取附加信息或者导致服务器响应的过程. Ajax广泛用于从服务器读取数据,并用所得到的数据更新页面,以及向服务器发送数据 Aja ...

  2. ThinkPHP URL模式和URL重写

    现在用的版本是TP3.1.3,这两天总是遇到NotFound的错误,解析路径错误,所以认真研究了一下手册,发现问题出在URL模式上面. URL模式 一般是使用U方法来生成路径,U方法的定义规则如下(方 ...

  3. InstallShield:自己备份

    LIST listData;//声明listData listData = ListCreate(STRINGLIST);//创建一个空的实际字符串或数字列表. //参数都是在上个界面中赋值,然后在下 ...

  4. codeforces 361 C - Mike and Chocolate Thieves

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Description Bad ...

  5. Asp.net MVC中三大描述对象之ActionDescriptor 以及继承类ReflectedControllerDescriptor

    ActionDescriptor抽象类中几个基本的属性: ControllerName:被描述的Controller名称,去除后缀Controller的名称.例如:HomeController则为Ho ...

  6. Maven入门详解以及Eclisp的集成

    1.首先要安装Maven到操作系统上 Maven的下载页面:http://maven.apache.org/download.html Maven跟Tomcat很像,下载下来后直接解压在指定的目录就安 ...

  7. “未能加载文件或程序集file:///E:/MoneySet.dll或它的某一个依赖项,试图加载格式不正确的程序,行203,位置5. 文件:MReportSet.resx”,

    http://bbs.csdn.net/topics/390334265 1.右键卸载项目2.右键选择编辑工程文件,在打开的文件的最后一行</project>之前加以下内容: <Pr ...

  8. tyvj P1075 - 硬币游戏 博弈DP

    P1075 - 硬币游戏 From price    Normal (OI)总时限:10s    内存限制:128MB    代码长度限制:64KB 背景 Background 农民John的牛喜欢玩 ...

  9. ios7新特性1-UI变化、UIKit动态行为支持与Text Kit新接口

    iOS 7.0新特性1 iOS 7的UI经过了重新设计.另外,iOS7中引入了新的动画系统,便于创建2D和2.5D的游戏.多任务支持提升,点对点通讯以及其他重要的特征使iOS7相对于以往的SDK来说发 ...

  10. Android 使用HTTP(get和post)方式登陆服务器

    package com.wuyou.submittoserver; import android.os.Bundle; import android.support.v7.app.ActionBarA ...