[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中 ...
随机推荐
- ArcGIS Api For Flex 动态画点和线
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- MVC—实现ajax+mvc异步获取数据
之前写过ajax和一般处理程序的结合实现前后台的数据交换的博客,如今做系统用到了MVC,同一时候也用到了异步获取数据. ajax+一般处理程序与MVC+ajax原理是一样的在"URL&quo ...
- apache2 虚拟机多用户多站点设置 mpm-itk
MPM设置 https://bbs.csdn.net/topics/390479795/ vim /opt/lampp/etc/extra/httpd-ssl.conf vim /opt/lampp/ ...
- Broadcast-广播的接收
至于广播的意思,不再赘述,直接看它的使用 先看代码 package com.example.test1123; import android.annotation.SuppressLint; impo ...
- Day4上午解题报告
预计分数:50 +0+0=50 实际分数:50+0+10=60 毒瘤出题人,T3不给暴力分 (*  ̄︿ ̄) T1 https://www.luogu.org/problem/show?pid=T155 ...
- SuSe Linux 10 企业服务器搭建双机集群配置实例
650) this.width=650;" onclick="window.open("http://blog.51cto.com/viewpic.php?refim ...
- 使用Vue脚手架(vue-cli)从零搭建一个vue项目(包含vue项目结构展示)
注:在搭建项目之前,请先安装一些全局的工具(如:node,vue-cli等) node安装:去node官网(https://nodejs.org/en/)下载并安装node即可,安装node以后就可以 ...
- BZOJ2329: [HNOI2011]括号修复(Splay)
解题思路: Replace.Swap.Invert都可以使用Splay完美解决(只需要解决一下标记冲突就好了). 最后只需要统计左右括号冲突就好了. 相当于动态统计最大前缀合和最小后缀和. 因为支持翻 ...
- 【Java学习】Font字体类的用法介绍
一.Font类简介 Font类是用于设置图形用户界面上的字体样式的,包括字体类型(例如宋体.仿宋.Times New Roman等).字体风格(例如斜体字.加粗等).以及字号大小. 二.Font类的引 ...
- vector转数组
vector转数组 由于vector内部的数据是存放在连续的存储空间,vector转数组事实上只需要获取vector中第一个数据的地址和数据的长度即可.如果仅仅是传参,无需任何操作,直接传地址即可,如 ...