Learn how to use the 'withReducer' higher order component using the alternative reducer form. If you like using reducers in redux, you’ll be able to reuse that same technique but for local state.

import React from 'react';
import {withReducer, withHandlers, compose} from 'recompose'; 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 User2 = ({ status, name }) => (
<div style={UserStyle}>
<Tooltip text="Cool Dude!">{name}</Tooltip>-
<Statue status={status}/>
</div>
); export default User2;

'withReducer' take (state_name, dispatch_function, reducer_function, init_state) as params.

最新文章

  1. HTML文档、javascript脚本的加载与解析
  2. redis批量删除key
  3. jdk研究——java.lang
  4. mysql GROUP BY 与 ORDER BY 查询不是最新记录
  5. CRUD之delete操作
  6. clang: error: no such file or directory: 报错
  7. MYSQL操作的一些知识点,持续更新中&#183;&#183;&#183;&#183;
  8. STL中的set容器的一点总结(转)
  9. A左右ndroid正在使用Uri监视数据库中的更改
  10. 排序算法的C#实现
  11. 小程序脚本语言WXS,你想要的都在这里了
  12. 动态流程图关于jointJs的使用
  13. &#127797;react小记 &#127797;
  14. UFLDL 教程学习笔记(二)反向传导算法
  15. 转:【专题十一】实现一个基于FTP协议的程序——文件上传下载器
  16. Mac下GitHub以及GitHub Desktop使用实战
  17. Mysql update case
  18. 关于初级dp的一些记忆
  19. 设置多个ip ,实现ip欺骗
  20. MyEclipse8.6启动后提示内存不足的解决方案(亲测,完美解决)

热门文章

  1. crmjs区分窗口是否是高速编辑
  2. thinkphp多层volist实现多表查询
  3. jQuery post 打开新窗口
  4. 深入理解Linux启动过程
  5. golang 逐行读取文件
  6. CISP/CISA 每日一题 16
  7. mysql 多实例案例实战
  8. python3对序列求绝对值
  9. nuxt按需引入 element-UI、自定义主题色(终极按需引入)
  10. Zookeeper源码用ant进行编译为eclipse工程--转载