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. UVALive-6485-Electric Car Rally(BFS)

    题目:点击打开链接 思路:对于当前位置的每个时间段都要走一遍(除了那些须要的时间比最大同意的时间还大的),用 整形 vis[当前位置][剩余油量] 标记. #include <cstdio> ...

  2. actionBarTab-actionBarTab自定义 布局没法改变其中字体相对中间的位置

    我们经常遇到对actionBarTab 进行操作的情况.现在记录修改它的样式的方法,已经如何自定义tab的显示布局 1.在你的theme主题中添加<item name="android ...

  3. 字符设备驱动-----Linux中断处理体系结构

    一.中断处理体系结构的初始化 Linux内核将所有的中断统一编号,使用一个irq_desc结构数组来描述这些中断;每个数组项对应一个中断,也可能是一组中断,它们共用相同的中断号,里面记录了中断的名称. ...

  4. Eclipse工具修理集

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 俗话说:工欲善其事.必先利其器,eclipse偶尔会发发脾气,那我们要记录下"她" ...

  5. android通用JSON解析

    ackage cn.com.pcgroup.<a href="http://lib.csdn.net/base/15" class="replace_word&qu ...

  6. Windows系统 配置Java的JDK环境变量

    安装了JDK或者绿色版后,在系统的环境变量设置中,进行以下配置: 1.新建->变量名"JAVA_HOME",变量值"D:\jdk1.8.0_05"(即JD ...

  7. ArcEngine的ToolbarControl解析

    转自Love Lyre原文 ArcEngine的ToolbarControl解析 ToolbarControlClass有三个主要的接口: IToolbarControl, IToolbarContr ...

  8. eclipse中的乱码问题

    在新建项目或导入工程时常常遇到的问题: 1.导入工程后,Java文件中文乱码 项目右键--Properties--Resource(快捷键Alt+Enter),在Text file encoding中 ...

  9. Garbage Collection Optimization for High-Throughput and Low-Latency Java Applications--转载

    原文地址:https://engineering.linkedin.com/garbage-collection/garbage-collection-optimization-high-throug ...

  10. 关于python的深浅拷贝、赋值

    https://blog.csdn.net/weixin_39750084/article/details/81435454