首页 > 代码库 > [React] Update State in React with Ramda's Evolve
[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 evolve
function to make our updater function a reusable utility that isn‘t tied to the React API.
//@flowimport React from ‘react‘;import {inc, dec, lensProp, over, evolve, multiply} from ‘ramda‘;// Using Lenseconst 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。