博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[React] Pass Data To Event Handlers with Partial Function Application
阅读量:4317 次
发布时间:2019-06-06

本文共 1364 字,大约阅读时间需要 4 分钟。

In this lesson we’ll see how to pass an item’s id value in an event handler and get the state to reflect our change. We’ll also create a helper function that allows us to use partial function application to clean up the event handler code and make it more “functional”

 

Previous code:

const ActionBtns = ({ selectedBox, onBtnClick }) => (    
);

 

We want to change the highlight code to partial applied function:

const ActionBtns = ({ selectedBox, onBtnClick }) => {    const setGreenColor = partial(onBtnClick, 'green', selectedBox);    const setRedColor = partial(onBtnClick, 'red', selectedBox);    return (        
);};

 

lib:

export const partial = (fn, ...args) => fn.bind(null, ...args);

 

Test:

import {
partial} from '../lib/util';const add = (a, b) => a + b;const addThree = (a,b,c) => a + b + c;test('partial applies the first argument ahead of time', () => { const inc = partial(add, 1); const result = inc(2); expect(result).toBe(3);});test('partial applies the multiple arguments ahead of time', () => { const inc = partial(addThree, 1, 2); const result = inc(3); expect(result).toBe(6);});

 

转载于:https://www.cnblogs.com/Answer1215/p/6361366.html

你可能感兴趣的文章
zabbix
查看>>
多线程基础
查看>>
完美解决 error C2220: warning treated as error - no ‘object’ file generated
查看>>
使用SQL*PLUS,构建完美excel或html输出
查看>>
前后台验证字符串长度
查看>>
《算法导论 - 思考题》7-1 Hoare划分的正确性
查看>>
win64 Python下安装PIL出错解决2.7版本 (3.6版本可以使用)
查看>>
获取各种类型的节点
查看>>
表达式求值-201308081712.txt
查看>>
centos中安装tomcat6
查看>>
从Vue.js窥探前端行业
查看>>
学习进度
查看>>
poj3368 RMQ
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>