[React] Recompose: Override Styles & Elements Types in React
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的更多相关文章
- 【React】383- React Fiber:深入理解 React reconciliation 算法
作者:Maxim Koretskyi 译文:Leiy https://indepth.dev/inside-fiber-in-depth-overview-of-the-new-reconciliat ...
- React.js入门笔记(续):用React的方式来思考
本文主要内容来自React官方文档中的"Thinking React"部分,总结算是又一篇笔记.主要介绍使用React开发组件的官方思路.代码内容经笔者改写为较熟悉的ES5语法. ...
- 五分钟学习React(三):纯HTML代码搭建React应用
上一期我们使用了React官方的脚手架运行React应用.大家可能会觉得这种方法很繁琐,需要配置各种第三方插件.JQuery时代的前端真是让人怀念.这一期,我就带领大家创建一个"怀旧版&qu ...
- React 源码剖析系列 - 不可思议的 react diff
简单点的重复利用已有的dom和其他REACT性能快的原理. key的作用和虚拟节点 目前,前端领域中 React 势头正盛,使用者众多却少有能够深入剖析内部实现机制和原理. 本系列文章希望通过剖析 ...
- React Native v0.4 发布,用 React 编写移动应用
React Native v0.4 发布,自从 React Native 开源以来,包括超过 12.5k stars,1000 commits,500 issues,380 pull requests ...
- React躬行记(5)——React和DOM
React实现了一套与浏览器无关的DOM系统,包括元素渲染.节点查询.事件处理等机制. 一.ReactDOM 自React v0.14开始,官方将与DOM相关的操作从React中剥离,组成单独的rea ...
- 没有用到React,为什么我需要import引入React?
没有用到React,为什么我需要import引入React? 本质上来说JSX是React.createElement(component, props, ...children)方法的语法糖. 所以 ...
- React前端有钱途吗?《React+Redux前端开发实战》学起来
再不学React就真的跟不上大前端的形式了,目前几乎所有前端的招聘条件都是精通React者优先,看看拉勾网的React薪资,都是15K-20K,这个暑假,必须动起来了. 如果你熟悉JavaScript ...
- React躬行记(16)——React源码分析
React可大致分为三部分:Core.Reconciler和Renderer,在阅读源码之前,首先需要搭建测试环境,为了方便起见,本文直接采用了网友搭建好的环境,React版本是16.8.6,与最新版 ...
随机推荐
- 18/9/9牛客网提高组Day1
牛客网提高组Day1 T1 中位数 这好像是主席树??听说过,不会啊... 最后只打了个暴力,可能是n2logn? 只过了前30% qwq #include<algorithm> #in ...
- Spring学习总结(8)——25个经典的Spring面试问答
1.什么是Spring框架?Spring框架有哪些主要模块? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台.Spring帮助开发者解决了开发中基础性的问题, ...
- 微信支付v2开发(9) 标记客户投诉处理状态
本文介绍微信支付中如何标记客户投诉的处理状态. 一.API Api 的 url 为: https://api.weixin.qq.com/payfeedback/update?access_token ...
- socket UDP简单通讯
// // SocketUDPServerClient.m // socket_server_client // // Created by lujunjie on 2016/11/26. // Co ...
- JSP与Servlet的介绍说明
什么是Servlet和JSP 用Java开发Web应用程序时用到的技术主要有两种,即Servlet和JSP. Servlet是在服务器端执行的Java程序,一个被称为Servlet容器的程序(其实就是 ...
- Nginx 虚拟主机及正向代理设置
添加虚拟主机 # vim /usr/local/nginx-1.9.0/conf/vhost/proxy.conf server { resolver 8.8.8.8; listen ; locat ...
- 2、HZK和FreeType的使用
HZK16汉字库的使用 定义如下: unsigned char str[]="我" 在运行时str被初始化为2个字节长度,内容为“我”的GBK码,为:0xCE(区码),0xD2(位 ...
- Shiro的Filter机制详解---源码分析(转)
Shiro的Filter机制详解 首先从spring-shiro.xml的filter配置说起,先回答两个问题: 1, 为什么相同url规则,后面定义的会覆盖前面定义的(执行的时候只执行最后一个). ...
- [Nuxt] Update State with Vuex Actions in Nuxt.js
You can conditionally add classes to Vue.js templates using v-bind:class. This will help display the ...
- [Ramda] Complement: Logic opposite function
Take a function as arguement, and the function only return true of false. If the function 'f' return ...