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;

最新文章

  1. 关键字static(1)
  2. static 类成员变量 和 static const类成员变量
  3. Java简单类(部门、领导、雇员关系)
  4. common.js
  5. 《NoSQL精粹》思维导图读书笔记
  6. Codeforces Round #303 (Div. 2) D 贪心
  7. 得到创建人Id
  8. power desinger 学习笔记三&lt;批量执行sql语句&gt;
  9. 使用 C# 进行 Outlook 2003 编程
  10. Linux笔记(固定USB摄像头硬件端口,绑定前后置摄像头)
  11. TensorFlow + Keras 实战 YOLO v3 目标检测图文并茂教程
  12. Heap
  13. Silverlight网页打开后马上崩溃,“白屏”,而且毫无提示
  14. jmeter测试mysql遇到的问题
  15. 使用ntlmrelayx在任何地方进行中继凭据
  16. L2-019. 悄悄关注
  17. JAVA基础部分复习(三、泛型)
  18. 伤不起:File.toPath() &amp; Paths.get()
  19. 使用新版SDK不想跳转微博客户端能否直接发送微博分享?
  20. InnoDB 引擎独立表空间

热门文章

  1. mysql 不能启动的两种错误提示及解决方法
  2. 使用缓存Memcache存储更新微信access token
  3. Java学习笔记七 常用API对象三
  4. 2、TaskFactory类
  5. angular 子组件与父组件通讯
  6. POJ 3009 Curling 2.0 {深度优先搜索}
  7. [Javascript AST] 1. Continue: Write a simple Babel plugin
  8. Skill of vim
  9. tomcat的一些简单配置
  10. 18. springboot整合jsp