[React] Update State in React with Ramda's Evolve

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 calls to use an updater function and then leverage Ramda‘s evolvefunction to make our updater function a reusable utility that isn‘t tied to the React API.

 

//@flow

import React from ‘react‘;
import {inc, dec, lensProp, over, evolve, multiply} from ‘ramda‘;

// Using Lense
const countLens = lensProp(‘count‘);
const increase = over(countLens, inc);
const decrease = over(countLens, dec);
const doubleCount = evolve({count: multiply(2)});

export default class Counter extends React.Component {

    state = {
        count: 0
    };

    increase = () => this.setState(increase);

    decrease = () => this.setState(decrease);

    double = () => this.setState(doubleCount);

    render() {
        return (
            <div>
                <button onClick={this.increase}>+</button>
                {this.state.count}
                <button onClick={this.decrease}>-</button>
                <button
                    disabled={this.state.count === 0}
                    onClick={this.double}
                >*2</button>
            </div>
        );
    }

}

 

以上是关于[React] Update State in React with Ramda's Evolve的主要内容,如果未能解决你的问题,请参考以下文章

react异常:Can‘t perform a React state update on an unmounted component[已解决]

react常见组件问题Can't perform a React state update on an unmounted component

[Nuxt] Update State with Vuex Actions in Nuxt.js

react项目Bug:组件销毁清除监听(Can't perform a React state update on an unmounted component.)

useFetch Can't perform a React state update on an unmounted component 警告

React 中,如果state中有一个较深层的值改变了,怎么去setState