[React] Update Component State in React With Ramda Lenses
In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. We'll create a lens to focus on the property we want to target and use over to apply the existing state value to a utility function and we'll get back a new state that will be reflected in our rendered UI.
We have code here, and we are going to improve the code:
//@flow import React from 'react';
import {inc, dec} from 'ramda'; export default class Counter extends React.Component { state = {
count: 0
}; increase = () => {
this.setState((prevState) => {
return {
count: inc(prevState.count)
};
})
}; decrease = () => {
this.setState((prevState) => {
return {
count: dec(prevState.count)
}
})
}; render() {
return (
<div>
<button onClick={this.increase}>+</button>
{this.state.count}
<button onClick={this.decrease}>-</button>
</div>
);
} }
First, we can use Ramda lense to update code, if the component's state contains only one prop, it is ideal to use lense to create reusable method:
import {inc, dec, lensProp, over} from 'ramda';
// Using Lense
const countLens = lensProp('count');
export default class Counter extends React.Component {
state = {
count: 0
};
increase = () => this.setState(
(prevState) => over(countLens, inc, prevState)
);
decrease = () => this.setState(
(prevState) => over(countLens, dec, prevState)
);
Because Ramda's method are auto currying, so we can write like:
increase = () => this.setState(over(countLens, inc));
decrease = () => this.setState(over(countLens, dec));
Second, it is recommended to pull out busniess logic out of component, so we can do:
//@flow import React from 'react';
import {inc, dec, lensProp, over} from 'ramda'; // Using Lense
const countLens = lensProp('count');
const increase = over(countLens, inc);
const decrease = over(countLens, dec); export default class Counter extends React.Component { state = {
count: 0
}; increase = () => this.setState(increase); decrease = () => this.setState(decrease); render() {
return (
<div>
<button onClick={this.increase}>+</button>
{this.state.count}
<button onClick={this.decrease}>-</button>
</div>
);
} }
[React] Update Component State in React With Ramda Lenses的更多相关文章
- [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 手稿 - Component state
Component state 实例: import React, { PureComponent } from 'react'; export default class extends PureC ...
- [React] Create component variations in React with styled-components and "extend"
In this lesson, we extend the styles of a base button component to create multiple variations of but ...
- [React] Update State in React with Ramda's Evolve
In this lesson we'll take a stateful React component and look at how we can refactor our setState ca ...
- [React] Update State Based on Props using the Lifecycle Hook getDerivedStateFromProps in React16.3
getDerivedStateFromProps is lifecycle hook introduced with React 16.3 and intended as a replacement ...
- [Mobx] Using mobx to isolate a React component state
React is great for diffing between Virtual-DOM and rendering it to the dom. It also offers a naïve s ...
- A Bite Of React(2) Component, Props and State
component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome ...
- React & update state with props & Object.assign
React & update state with props & Object.assign Object.assign({}, oldObj, newObj) https://re ...
- [React] React Fundamentals: Component Lifecycle - Mounting Basics
React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...
随机推荐
- SQL2012的新分页方法
SELECT BusinessEntityID , FirstName , LastName FROM Person.Person ORDER BY BusinessEntityID OFFSET ( ...
- Vue Invalid handler for event "": got undefined
原因:绑定的方法不是放在methods:{}里.比如我把绑定的函数写在了computed:{}里就会报这个错.
- Jmeter使用_处理响应结果显示乱码
1. 添加BeanShell PostProcessor 输入prev.setDataEncoding("utf-8"); 目的是修改响应数据编码格式为utf-8,保存
- 词向量 word2vec
看的这一篇的笔记 http://licstar.net/archives/328 看不太懂. 要学的话,看这里吧,这里把一些资料做了整合: http://www.cnblogs.com/wuzhitj ...
- 常见c#正则表达式类学习整理
1.MatchCollection类 用于输入字符串所找到的成功匹配的集合,Regex.Matches 方法返回 MatchCollection 对象 用法 //str:要搜索匹配项的字符串 patt ...
- ED/EP系列1《简单介绍》
电子存折(ED:ElectronicDeposit)一种为持卡人进行消费.取现等交易而设计的支持个人识别码(PIN)保护的金融IC卡应用. 它支持圈存.圈提.消费和取现等交易. 电子钱包(EP:Ele ...
- php图像处理(thinkphp框架有相对强大的图像处理功能)
php图像处理(thinkphp框架有相对强大的图像处理功能) 一.总结 1.php处理图像:php处理图像需要安装外库(gd库) 2.gd库函数可以非常完美的操作图像:安装好库之后,这个库里面的函数 ...
- Css 显示删除条目效果
样式设置
- VC中画矩形框 & polyline画多点
搞自动化会经常遇到一个问题就是记录实时的曲线,通常做法是首先将数据保存在一个记事本中,或数据库.使用VB或DELPHI可以直接调用现成的控件画图,只是控制起来不方便.所以使用VC就需要程序来控制.在网 ...
- 彩票案例-frame,center和bounds属性
控件的属性: 二.frame.center和bounds属性 " 在iOS中,每一个控件都是继承于UIView的.都会有视图的属性存在,控制这个视图的位置就有Frame和Bounds两个属性 ...