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的更多相关文章

  1. [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 ...

  2. React 手稿 - Component state

    Component state 实例: import React, { PureComponent } from 'react'; export default class extends PureC ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. A Bite Of React(2) Component, Props and State

    component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome ...

  8. React & update state with props & Object.assign

    React & update state with props & Object.assign Object.assign({}, oldObj, newObj) https://re ...

  9. [React] React Fundamentals: Component Lifecycle - Mounting Basics

    React components have a lifecycle, and you are able to access specific phases of that lifecycle. Thi ...

随机推荐

  1. Android学习笔记进阶十二之裁截图片

    package xiaosi.cut; import java.io.File; import android.app.Activity; import android.content.Intent; ...

  2. 3.JPA开发

    转自:https://blog.csdn.net/aspnetandjava/article/details/7034779 1. 什么是JPA 1. JPA(Java Persistence API ...

  3. mysql 实行模糊查询 一个输入值匹配多个字段和多个输入值匹配一个字段

    mysql 实行模糊查询  一个输入值匹配多个字段 MySQL单表多字段模糊查询可以通过下面这个SQL查询实现 为啥一定要150字以上  真的麻烦  还不让贴代码了 SELECT * FROM `ma ...

  4. BZOJ4196: [Noi2015]软件包管理器(树链剖分)

    Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...

  5. 异步调用WCF的方法需要小心的地方

    直接使用下面的代码,由于client对象占用的资源没有被释放,会导致内存泄露GetSimServiceReference.GetSimServiceClient client = new GetSim ...

  6. asp.net 查询sql数据表的网页模板

    最近因为工作需求,要制作一个网页模板,主要是用于快速开发,可以查询Sql数据表信息的模板, 昨天做好了,这个只是一个Demo,但是功能已经齐全了, 开发新的网站时,需要新增一个xml,复制粘贴网页的前 ...

  7. ASM(四) 利用Method 组件动态注入方法逻辑

    这篇继续结合样例来深入了解下Method组件动态变更方法字节码的实现.通过前面一篇,知道ClassVisitor 的visitMethod()方法能够返回一个MethodVisitor的实例. 那么我 ...

  8. 多线程中的&quot;断点&quot;续传《notify()和wait()》

    眼下在做一个项目.关于软件管理与下载的,预计项目提交日期定在6月9号.项目做了有20天了,可是在一个功能上卡住了.在这个项目中有一个功能----APK的下载须要实现. 相信大家都玩过非常多关于下载AP ...

  9. 早该知道的 7 个JavaScript 技巧[转]

    简洁写法 对象的简写在过去,如果你想创建一个对象,你需要这样: var car = new Object();  car.colour = 'red';  car.wheels = 4;  car.h ...

  10. Android Studio运行报错,Cannot find System Java Compiler. Ensure that you have installed a JDK......

    详细报错信息如下 Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Cannot find System J ...