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. 数学定理证明机械化的中国学派(II)

    所谓"学派"是指:存在一帮人,具有同样或接近的学术观点或学术立场,採用某种特定的"方法"(或途径),在一个学术方向上共同开展工作.而且做出了相当有迎影响的学术成 ...

  2. dot-files/directories 点开头的文件或文件夹(windows/linux)

    What's so special about directories whose names begin with a dot? 不管是 windows 系统,还是类 linux 系统,以点开头的文 ...

  3. [Firebase] Firebase Cloud Functions

    Firebase cloud functions is similar to AWS lambda or serverless. You can deploy you code which wrote ...

  4. OpenWrt配置绿联的usb转Ethernet网口驱动

    这个选择kernel modules中的kmod-usb-net-asix 须要加入网络设备接口.相似建立一个vlan,配置下防火墙之类的.

  5. 启动Tomcat,startup.bat一闪而过的解决办法

    1.打开命令行:win+R --> cmd2.将解压后的tomcat\bin\startup.bat文件拖到控制台窗口中,回车. 这样就可以看到错误信息的提示,根据提示修改即可.

  6. 将exe添加到windows服务中

    mongod --dbpath D:\MongoDB\data --logpath=D:\MongoDB\logs\mongodb.log --install http://www.cnblogs.c ...

  7. sql基础知识集锦

    Sql常用语法 下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT ...

  8. JS实现穿墙效果(判断鼠标划入划出的方向)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. android 5.x system.img 大于2G导致编译otapackage时报错怎样处理

    当system分区预制过多apk时假设img size超过2G 在make otapackage时会报例如以下错误  zipfile.LargeZipFile: Zipfile size would ...

  10. java学习顺序

    作者:阿弎 链接:http://www.zhihu.com/question/19851109/answer/91201815 来源:知乎 著作权归作者所有,转载请联系作者获得授权. ======== ...