[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 state to—and create a reusable local state pattern for—your functional stateless components. No need for classes!
withState:
const Statue = withState('show', 'toggleShow', false)(
({ status, show, toggleShow }) =>
(<span onClick={() => toggleShow((val) => !val)}>
{status}
{show && <StatusList/>}
</span>)
);
withState, we create a variable 'show' and a function 'toggleShow', the default value for 'show' is false. Now the variable and function are injected into our stateless component as props.
The function 'toggleShow' can just take a value as arguement or it can also take function as an arguement.
take as function:
onClick={() => toggleShow((val) => !val)}
take as value:
onClick={() => toggleShow(!show)}
withHandlers & compose:
To make withState more reuseable, we can use 'withHandler' & 'compose' to create an HoC.
const withToggle = compose(
withState('toggleState', 'toggleShow', false),
withHandlers({
toggle: ({toggleShow}) => (e) => toggleShow((val) => !val),
show: ({toggleShow}) => (e) => toggleShow(true),
hide: ({toggleShow}) => (e) => toggleShow(false)
})
); 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>
));
[Recompose] Add Local State to a Functional Stateless Component using Recompose的更多相关文章
- [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 ...
- [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 ...
- 【待整理】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 ...
- [React] Update Application State with React Apollo ApolloConsumer Component
In this lesson I refactor some code that utilizes the Mutation component to update client-side cache ...
- React报错:Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix,
今天在开发时报了以下错误,记录一下 我们不能在组件销毁后设置state,防止出现内存泄漏的情况 出现原因直接告诉你了,组件都被销毁了,还设置个锤子的state啊 解决方案: 利用生命周期钩子函数:co ...
- [React & Debug] Quick way to debug Stateless component
For example we have the following code: const TodoList = (props) => ( <div className="Tod ...
- [Preact] Use State and Props in the Component Render Function
Preact offers, in addition to the regular component API from React, the ability to access both props ...
- 【React踩坑记三】React项目报错Can't perform a React state update on an unmounted component
意思为:我们不能在组件销毁后设置state,防止出现内存泄漏的情况 分析出现问题的原因: 我这里在组件加载完成的钩子函数里调用了一个EventBus的异步方法,如果监听到异步方法,则会更新state中 ...
- 【React踩坑记三】React项目报错Can't perform a React state update on an unmounted component
意思为:我们不能在组件销毁后设置state,防止出现内存泄漏的情况 分析出现问题的原因: 我这里在组件加载完成的钩子函数里调用了一个EventBus的异步方法,如果监听到异步方法,则会更新state中 ...
随机推荐
- 洛谷 P3385 【模板】负环
P3385 [模板]负环 题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M ...
- hdu 1003 Max Sum 最大字段和 dp
今天看了一上午dp.看不太懂啊.dp确实不简单.今天開始学习dp,搜了杭电的dp46道,慢慢来吧.白书上的写的 又不太具体,先写几道题目再说. .. 题目连接:id=516&page=1&qu ...
- 安卓使用WebView下载文件,安卓实现软件升级功能
由于调用系统默认浏览器下载更新,造成用户体验非常不好,所以决定在webview中直接下载系统更新.然后直接安装. 由于要下载,所以必须用webview,联网权限这里不说了,直接写在manifafest ...
- 爬虫爬数据时,post数据乱码解决的方法
近期在写一个爬虫,目标站点是:http://zx.bjmemc.com.cn/.可能是为了防止被爬取数据,它给自身数据加了密. 用谷歌自带的抓包工具也不能捕获到数据. 于是下了Fiddler. ...
- cocos2d-x 一些3效果的类及创建參数
CCShaky3D::create(时间,晃动网格大小,晃动范围,Z轴是否晃动); //创建一个3D晃动的效果 CCShakyTiles3D::create(时间,晃动网格大小,晃动范围,Z轴是否晃动 ...
- 17.Node.js 回调函数--异步编程
转自:http://www.runoob.com/nodejs/nodejs-tutorial.html Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程 ...
- Web页面转换成Word文件,利用wordXML
简介:处理流程表单数据以WordXML形式填充Word文档表格换行符丢失问题 //将前台收集的XML中“$”循环拆分成"<w:br/>" by pengyc 解决表格填 ...
- 1.3 Quick Start中 Step 3: Create a topic官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 3: Create a topic Step 3: 创建一个主题(topi ...
- go每个函数写代码例子
https://github.com/astaxie/gopkg 由于目前golang的手册里面针对函数的例子太少了,很多时候不知道怎么使用,好多人都是看源代码才明白怎么用,这个给我们快速开发gola ...
- IOIOI卡片占卜(Atcoder-IOIOI カード占い)(最短路)
题目描述: K 理事長は占いが好きで,いつも様々な占いをしている.今日は,表の面に ‘I’ が,裏の面に ‘O’ が書か れたカードを使って今年の IOI での日本選手団の出来を占うことにした. 占い ...