[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 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的更多相关文章
- [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 ...
- 【待整理】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 ...
- [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 ...
- [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 ...
- 关于props和state以及redux中的state
React的数据模型分为共有数据和私有数据,共有数据可以在组件间进行传递,私有数据为当前组件私有.共有数据在React中使用props对象来调用,它包含标签所有的属性名称和属性值,props对象有三个 ...
- 前端(十一):props、state及redux关系梳理
所谓状态机,是一种抽象的数据模型,是“事物发展的趋势”,其原理是事件驱动.广泛地讲,世界万物都是状态机. 一.状态机是一种抽象的数据模型 在react中,props和state都可以用来传递数据.这里 ...
- [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 ...
- React修改state(非redux)中数组和对象里边的某一个属性的值
在使用React时,会经常需要处理state里边设置的初始值以达到我们的实际需求,比如从接口获取到列表数据后要赋值给定义的列表初始值,然后数据驱动view视图进而呈现在我们眼前,这种最简单的赋值方式实 ...
- [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 ...
随机推荐
- [Java开发之路](16)学习log4j日志
1. 新建一个Javaproject.导入Jar包(log4j-1.2.17.jar) Jar包下载地址:点击打开链接 2. 配置文件:创建并设置log4j.properties # 设置 log4j ...
- 深度学习 Deep LearningUFLDL 最新Tutorial 学习笔记 2:Logistic Regression
1 Logistic Regression 简述 Linear Regression 研究连续量的变化情况,而Logistic Regression则研究离散量的情况.简单地说就是对于推断一个训练样本 ...
- actionBar-shareIcon 分享按钮的修改
今天为了修改图库的分享按钮,进行了很多的尝试 1.寻找到了xml文件,如下 <?xml version="1.0" encoding="utf-8"?&g ...
- js配合My97datepicker给日期添加天数
<input name="ctl00$ContentPlaceHolder1$txtTimeStart" type="text" value=" ...
- modal模态框插件
用法: <!--模态框--> <div class="modal fade" id="myModal"> <div class=& ...
- windows 静态和动态库
c++中共有两种库:1.动态链接库LIB包含了函数所在的DLL文件和文件中函数位置的信息(入口),代码由运行时加载在进程空间中的DLL提供,称为动态链接库dynamic link library.(这 ...
- 【Educational Codeforces Round 35 D】Inversion Counting
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 排列中交换任意两个数字. 排列的逆序对个数的奇偶性会发生变化. 翻转这个过程其实就是len/2对数字发生交换. 交换了偶数次的话,不 ...
- 洛谷 P1755 斐波那契的拆分
P1755 斐波那契的拆分 题目背景 无 题目描述 已知任意一个正整数都可以拆分为若干个斐波纳契数,现在,让你求出n的拆分方法 输入输出格式 输入格式: 一个数t,表示有t组数据 接下来t行,每行一个 ...
- domino web app
近期在做个项目内容是:原企业的OA仅仅能在电脑桌面处理流程,不能在手机上处理审核功能,但随着企业需求,管理者需求在随时使用手机审核文档,达到及时处理文档及流程的及时性. 要求:1) ...
- matlab 文件路径问题
1. fullfile:路径补全 f = fullfile(filepart1,...,filepartN) 显然可变参数之间填充的路径分隔符(path separator),会根据操作系统而变化: ...