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.

[Recompose] Add Local State with Redux-like Reducers 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. 【待整理】MySQL alter table modify vs alter table add产生state不一样

    MySQL:5.6.35 OS:redhat5.8 今天更新数据库某些表字段,有如下两SQL: ①alter table xx modify xxxx;(表大概是77w) ②alter table s ...

  3. [Functional Programming ADT] Adapt Redux Actions/Reducers for Use with the State ADT

    By using the State ADT to define how our application state transitions over time, we clear up the ne ...

  4. [Redux] Accessing Dispatch and State with Redux -- connect

    If you have props and actions, you want one component to access those props and actions, one solutio ...

  5. 关于props和state以及redux中的state

    React的数据模型分为共有数据和私有数据,共有数据可以在组件间进行传递,私有数据为当前组件私有.共有数据在React中使用props对象来调用,它包含标签所有的属性名称和属性值,props对象有三个 ...

  6. 前端(十一):props、state及redux关系梳理

    所谓状态机,是一种抽象的数据模型,是“事物发展的趋势”,其原理是事件驱动.广泛地讲,世界万物都是状态机. 一.状态机是一种抽象的数据模型 在react中,props和state都可以用来传递数据.这里 ...

  7. [Recompose] Add Lifecycle Hooks to a Functional Stateless Component using Recompose

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

  8. React修改state(非redux)中数组和对象里边的某一个属性的值

    在使用React时,会经常需要处理state里边设置的初始值以达到我们的实际需求,比如从接口获取到列表数据后要赋值给定义的列表初始值,然后数据驱动view视图进而呈现在我们眼前,这种最简单的赋值方式实 ...

  9. [Recompose] Handle React Events as Streams with RxJS and Recompose

    Events are the beginning of most every stream. Recompose provides a createEventHandler function to h ...

随机推荐

  1. 跟着鬼哥学so改动,三,作业篇

    作业: 通过前面两篇文章的学习.请自行分析此应用,将当前用户类型改动为Gold Vip 用户. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3VpZ3V ...

  2. scroolspy滚动监听插件

    <nav id="nav" class="navbar navbar-default"> <a href="#" clas ...

  3. UVA 12333 Revenge of Fibonacci

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. Linux中删除文件,磁盘空间未释放问题追踪

    在客户使用我们产品后,发现一个问题:在删除了文件后.磁盘空间却没有释放.是有进程在打开这个文件,还是其它情况?我们一起来看看一下两个场景 一. 场景一:进程打开此文件 当一个文件正在被一个进程使用时. ...

  5. HTML基础-第二讲

    转自:https://blog.csdn.net/likaier/article/details/326657 我们在第一讲里概括了一下网页的主要框架,现在我们来详细的研究网页的内部细则: 先从它的身 ...

  6. jquery中prop()和attr()的使用

    jquery1.6+出现的prop()方法. • 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法. • 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法. • ...

  7. python关于字典的操作

    https://www.cnblogs.com/RENQIWEI1995/p/7931374.html 最常用的代码举例: dict = {'Name': 'Zara', 'Age': 7, 'Cla ...

  8. 【例题 7-9 UVA-1601】The Morning after Halloween

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 对于没有出现的,当成0节点就好. 所以总是认为有3个人需要走到各自的终点. 将平面图转成点边图.这样比较好枚举. (二维变成一维,模 ...

  9. [读书笔记]Java类载入过程

    一. 类的生命周期 类从被载入到虚拟机内存中開始,到卸载出内存为止,有下面(如图)的生命周期: 以上"载入->验证->准备->解析->初始化"称为类的载入过 ...

  10. [D3] Add label text

    If we want to add text to a node or a image // Create container for the images const svgNodes = svg ...