When we move from CSS to defining styles inside components we lose the ability to override styles with an external style sheet. We also lose the ability add the same class to different types of elements to style them consistently. Recompose allows us to regain the ability of override styles and to apply the same styles to different types of elements.

Feeling using Recompose is a good way to build npm module, custom libaray.

So the comoponent can be highly custmizable.

import React from 'react';
import {setDisplayName, componentFromProp, mapProps, defaultProps, compose} from 'recompose';
import Radium from 'radium'; const addStyle = style => mapProps(props => ({
...props,
style: [
props.style,
style
]
})); const style = {
backgroundColor: 'black',
color: 'white',
transform: 'rotate(-13deg)',
transition: 'all ease 0.43s',
':hover': {
backgroundColor: 'orange',
color: 'lightGreen',
transform: 'rotate(0deg)'
}
}; const enhance = compose(
addStyle(style),
setDisplayName('Button'),
defaultProps({
element: 'button'
}),
Radium
); export default enhance(componentFromProp('element'));

Using:

        <Button
element={'button'}
style={{borderRadius: '10px'}}
onClick={onRemoveBox}
>Remove</Button>

In the code, we are able to override the default style by passing 'style' from props:

const addStyle = style => mapProps(props => ({
...props,
style: [
props.style,
style
]
})); const enhance = compose(
addStyle(style),
setDisplayName('Button'),
defaultProps({
element: 'button'
}),
Radium
);

Here we also using 'Radium', so that we can add 'hover' sudo effect.

We can also say what kind of element we want to show:

    defaultProps({
element: 'button'
}), <Button
element={'a'} href="http://google.fi"
style={{borderRadius: '10px'}}
onClick={onRemoveBox}
>Remove</Button>

We can change to 'a' tag if we want.

[React] Recompose: Override Styles & Elements Types in React的更多相关文章

  1. 【React】383- React Fiber:深入理解 React reconciliation 算法

    作者:Maxim Koretskyi 译文:Leiy https://indepth.dev/inside-fiber-in-depth-overview-of-the-new-reconciliat ...

  2. React.js入门笔记(续):用React的方式来思考

    本文主要内容来自React官方文档中的"Thinking React"部分,总结算是又一篇笔记.主要介绍使用React开发组件的官方思路.代码内容经笔者改写为较熟悉的ES5语法. ...

  3. 五分钟学习React(三):纯HTML代码搭建React应用

    上一期我们使用了React官方的脚手架运行React应用.大家可能会觉得这种方法很繁琐,需要配置各种第三方插件.JQuery时代的前端真是让人怀念.这一期,我就带领大家创建一个"怀旧版&qu ...

  4. React 源码剖析系列 - 不可思议的 react diff

      简单点的重复利用已有的dom和其他REACT性能快的原理. key的作用和虚拟节点 目前,前端领域中 React 势头正盛,使用者众多却少有能够深入剖析内部实现机制和原理. 本系列文章希望通过剖析 ...

  5. React Native v0.4 发布,用 React 编写移动应用

    React Native v0.4 发布,自从 React Native 开源以来,包括超过 12.5k stars,1000 commits,500 issues,380 pull requests ...

  6. React躬行记(5)——React和DOM

    React实现了一套与浏览器无关的DOM系统,包括元素渲染.节点查询.事件处理等机制. 一.ReactDOM 自React v0.14开始,官方将与DOM相关的操作从React中剥离,组成单独的rea ...

  7. 没有用到React,为什么我需要import引入React?

    没有用到React,为什么我需要import引入React? 本质上来说JSX是React.createElement(component, props, ...children)方法的语法糖. 所以 ...

  8. React前端有钱途吗?《React+Redux前端开发实战》学起来

    再不学React就真的跟不上大前端的形式了,目前几乎所有前端的招聘条件都是精通React者优先,看看拉勾网的React薪资,都是15K-20K,这个暑假,必须动起来了. 如果你熟悉JavaScript ...

  9. React躬行记(16)——React源码分析

    React可大致分为三部分:Core.Reconciler和Renderer,在阅读源码之前,首先需要搭建测试环境,为了方便起见,本文直接采用了网友搭建好的环境,React版本是16.8.6,与最新版 ...

随机推荐

  1. Mybaits中session的应用一

    获取一级缓存session SqlSession session = this.yangchebaoDbManagerImpl.getSqlSessionFactory().openSession(f ...

  2. 一起talk C栗子吧(第九十八回:C语言实例--使用消息队列进行进程间通信二)

    各位看官们,大家好,上一回中咱们说的是使用消息队列进行进程间通信的样例.这一回咱们接着上一回的内容继续说使用消息队列进行进程间通信.闲话休提.言归正转.让我们一起talk C栗子吧! 我们在上一回中介 ...

  3. 执行spark-shell时遇到的主机地址的错误

    下载了spark 1.4,执行spark-shell时遇到以下错误: java.net.UnknownHostException: UKON-M-Q0EP: UKON-M-Q0EP: nodename ...

  4. C8815 用 USB网卡(Asix AX88772 )上网

    C8815 用 USB网卡(Asix AX88772 )上网 C8815不支持给USB外设供电,不过可以使用自供电的OTG线带动USB设备 C8815最新固件中没有Asix AX88772驱动,需要自 ...

  5. 洛谷 P2790 ccj与zrz之积木问题

    P2790 ccj与zrz之积木问题 题目背景 ccj和zrz无聊到了玩起了搭积木...(本题选自uva101,翻译来自<算法竞赛入门经典2>) 题目描述 从左到右有n个木块,编号从0到n ...

  6. 洛谷 P1497 木牛流马

    P1497 木牛流马 题目描述 孔明造出了木牛流马 木牛者,方腹曲头,一脚四足,头入领中,舌著于腹.载多而行少,宜可大用,不可小使:特行者数十里,群行者二十里也.曲者为牛头,双者为牛脚,横者为牛领,转 ...

  7. $OEM$文件夹的使用 (By无约而来)

    WIN7-OEM资料包中的目录都是以$OEM$文件夹出现的.比$OEM$高一级的目录,我通常是用来表示下一级的$OEM$的属性,例如,X64_ADMIN_LOADER表示此目录下的$OEM$文件夹是用 ...

  8. 深入理解线程本地变量ThreadLocal

    ThreadLocal理解: 假设在多线程并发环境中.一个可变对象涉及到共享与竞争,那么该可变对象就一定会涉及到线程间同步操作,这是多线程并发问题. 否则该可变对象将作为线程私有对象,可通过Threa ...

  9. boost 库编译选项

    boost大部分库仅仅须要包括头文件就可以使用,而有部分须要编译的.例如以下: E:\Qt\Qt3rdlib\boost_1_58_0>bjam --show-libraries The fol ...

  10. 利用朴素贝叶斯(Navie Bayes)进行垃圾邮件分类

    贝叶斯公式描写叙述的是一组条件概率之间相互转化的关系. 在机器学习中.贝叶斯公式能够应用在分类问题上. 这篇文章是基于自己的学习所整理.并利用一个垃圾邮件分类的样例来加深对于理论的理解. 这里我们来解 ...