Learn how to use the 'lifecycle' higher-order component to conveniently use hooks without using a class component.

import React from 'react';
import { withReducer, withHandlers, compose, lifecycle } from 'recompose'; // Mock Configuration
function fetchConfiguration() {
return new Promise((resolve) => {
setTimeout(() => resolve({
showStatus: true,
canDeleteUsers: true
}), );
});
}
const withConfig = lifecycle({
getInitialState(){
return { config: {} };
},
componentDidMount() {
fetchConfiguration()
.then((config) => {
this.setState({ config })
})
}
}) const UserStyle = {
position: 'relative',
background: 'lightblue',
display: 'inline-block',
padding: '10px',
cursor: 'pointer',
marginTop: '50px'
}; const StatusListStyle = {
background: '#eee',
padding: '5px',
margin: '5px 0'
}; const TooltipStyle = {
fontSize: '10px',
position: 'absolute',
top: '-10px',
width: '80px',
background: '#666',
color: 'white',
textAlign: 'center'
}; const StatusList = () =>
<div style={StatusListStyle}>
<div>pending</div>
<div>inactive</div>
<div>active</div>
</div>; const withToggle = compose(
withReducer('toggleState', 'dispatch', (state = false, action) => {
switch( action.type ) {
case 'SHOW':
return true;
case 'HIDE':
return false;
case 'TOGGLE':
return !state;
default:
return state;
}
}, false),
withHandlers({
toggle: ({ dispatch }) => (e) => dispatch({ type: 'TOGGLE' }),
show: ({ dispatch }) => (e) => dispatch({ type: 'SHOW' }),
hide: ({ dispatch }) => (e) => dispatch({ type: 'HIDE' })
})
); const Statue = withToggle(
({ status, toggle, toggleState }) =>
(<span onClick={() => toggle(!toggleState)}>
{status}
{toggleState && <StatusList/>}
</span>)
); const Tooltip = withToggle(({ show, hide, toggleState, text, children }) => (
<span>
<span>
{toggleState && <div
style={TooltipStyle}>
{ text }
</div>}
<span
onMouseOver={show}
onMouseOut={hide}
>
{ children }
</span>
</span>
</span>
)); const User3 = withConfig(({ status, name, config }) => (
<div style={UserStyle}>
<Tooltip text="Cool Dude!">{name}</Tooltip>-
{config.showStatus && <Statue status={status}/>}
{config.canDeleteUsers && <button>X</button> }
</div>
)); export default User3;

[Recompose] Add Lifecycle Hooks to a Functional Stateless Component using Recompose的更多相关文章

  1. [Recompose] Add Local State to a Functional Stateless Component using Recompose

    Learn how to use the 'withState' and 'withHandlers' higher order components to easily add local stat ...

  2. Ionic 4 and the Lifecycle Hooks

    原文: https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864 ------------------- ...

  3. Kubernetes Container lifecycle hooks

    简介 在kubernetes中,容器hooks旨在解决服务进程启动与停止时的一些优雅操作需求.比如说进程停止时要给进程一个清理数据的时间,保证服务的请求正常结束,而不是强行中断服务的运行,这样在生产环 ...

  4. [Recompose] Add Local State with Redux-like Reducers using Recompose

    Learn how to use the 'withReducer' higher order component using the alternative reducer form. If you ...

  5. [React & Debug] Quick way to debug Stateless component

    For example we have the following code: const TodoList = (props) => ( <div className="Tod ...

  6. [Recompose] Render Nothing in Place of a Component using Recompose

    Learn how to use the ‘branch’ and ‘renderNothing’ higher-ordercomponents to render nothing when a ce ...

  7. [React] displayName for stateless component

    We can use 'displayName' on component to change its component tag in dev tool: import React from 're ...

  8. [Recompose] Configure Recompose to Build React Components from RxJS Streams

    Recompose provides helper functions to stream props using an Observable library of your choice into ...

  9. Vue.js 系列教程 3:Vue-cli,生命周期钩子

    原文:intro-to-vue-3-vue-cli-lifecycle-hooks 译者:nzbin 这是 JavaScript 框架 Vue.js 五篇教程的第三部分.在这一部分,我们将学习 Vue ...

随机推荐

  1. 优化HTML代码的多种技巧

    怎样提升Web页面的性能,非常多开发者从多个方面来下手如JavaScript.图像优化.server配置,文件压缩或是调整CSS. 非常显然HTML 已经达到了一个瓶颈.虽然它是开发Web 界面必备的 ...

  2. Windows 98 二十岁了,这些功能都是从它开始的(虽然 Windows 98 不如 Windows 95 那样具有革命性,但完成度更高,更加成熟。到最后还是:相见不如怀念。)

    1998 年 6 月 25 日午夜,美国著名连锁零售店 CompUSA 门外挤满了狂热的消费者和媒体,他们在等待一款软件发售:Windows 98,即使明知它要到当天早上才正式上市. ▲ 在 Comp ...

  3. Windows Embedded POSready2009

    Windows Embedded POSready2009 ,这个看上去和 XP 差不多,可能是别人说的 XPE 系统 下载 POSready2009_CD.iso, 安装 KEY :         ...

  4. GridView-属性大全

    这是个网格控件 他的实现也是通过adapter来实现的,感觉跟listview在使用上并没有多大的区别 常见属性如下 1.android:numColumns=”auto_fit” //GridVie ...

  5. golang sync.WaitGroup

    //阻塞,直到WaitGroup中的所以过程完成. import ( "fmt" "sync" ) func wgProcess(wg *sync.WaitGr ...

  6. golang 函数传值

    package main import ( "fmt" ) type Vertex struct { X, Y int } func dop(a Vertex, vl int) { ...

  7. HDU 1007 Quoit Design 平面内最近点对

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...

  8. LAMP漫长编译安装

    传说中的LAMP :Linux+Apache+MySQL+Python/Perl/PHP,Linux上安装方式:RPM和源码安装,源码安装要依赖包安装,可以定制安装 首先安装编译工具 yum inst ...

  9. drawable-图片绘制

    首先看一下,下端代码 private Bitmap createSelectedChip(RecipientEntry contact, TextPaint paint) { int height = ...

  10. 手撕面试题ThreadLocal!!!

    说明 面试官:讲讲你对ThreadLocal的一些理解. 那么我们该怎么回答呢????你也可以思考下,下面看看零度的思考: ThreadLocal用在什么地方? ThreadLocal一些细节! Th ...