[React] Pass Data To Event Handlers with Partial Function Application
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 }) => (
<nav className={classnames('nav')}>
<RaisedButton
label="Red"
style={style}
onClick={() => onBtnClick('red', selectedBox)}/>
<RaisedButton
label="Green"
style={style}
onClick={() => onBtnClick('green', selectedBox)}/>
</nav>
);
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 (
<nav className={classnames('nav')}>
<RaisedButton
label="Red"
style={style}
onClick={setRedColor}/>
<RaisedButton
label="Green"
style={style}
onClick={setGreenColor}/>
</nav>
);
};
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, );
const result = inc();
expect(result).toBe();
}); test('partial applies the multiple arguments ahead of time', () => {
const inc = partial(addThree, , );
const result = inc();
expect(result).toBe();
});
[React] Pass Data To Event Handlers with Partial Function Application的更多相关文章
- partial function
[partial function] functools.partial(func[,*args][, **keywords]) Return a new partial object which w ...
- 事件处理(Event Handlers) ng-click操作
事件处理(Event Handlers) ng-click操作 step 10 本文主要通过介绍ng-click方法来对angularjs中的事件处理方法做个了解. 1.切换目录 git checko ...
- iphone dev 入门实例2:Pass Data Between View Controllers using segue
Assigning View Controller Class In the first tutorial, we simply create a view controller that serve ...
- 1.6.2 Uploading Data with Index Handlers
1.Uploading Data with Index Handlers 索引处理器就是Request Handlers,用于添加,更新,删除索引中的文档.另外,使用Tika抽取富文档数据,使用Dat ...
- react & redux data flow diagram
react & redux data flow diagram Redux 数据流程图
- [转]Creating an Entity Framework Data Model for an ASP.NET MVC Application (1 of 10)
本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/creating-a ...
- Scala之偏函数Partial Function
https://blog.csdn.net/bluishglc/article/details/50995939 从使用case语句构造匿名函数谈起在Scala里,我们可以使用case语句来创建一个匿 ...
- Python partial function 偏函数
Partial function 偏函数是将所要承载的函数作为partial()函数的第一个参数,原函数的各个参数依次作为partial()函数后续的参数,除非使用关键字参数. 当函数的参数个数太多, ...
- Scala Partial Function从官方文档解析
A partial function of type PartialFunction[A, B] is a unary function where the domain does not neces ...
随机推荐
- redirect_uri 参数错误
http://www.cnblogs.com/zitjubiz/p/5935712.html http://blog.csdn.net/u014033756/article/details/52038 ...
- thinkphp 整合 swiftmailer 实现邮件发送
thinkphp swiftmailer(phpmailer) 文件夹结构 图 1 swiftmailer-phpmailer 将swiftmailer整合到thinkphp中.如上图 1 我下载的版 ...
- 删除online日志測试及ora-600 [4194]错误的处理
今天做了一个关于破坏online日志的恢复測试,主要三个场景: 測试1:正常关闭数据库后删除非当前日志 測试2:正常关库后.删除在线日志文件 測试3:非正常关闭数据库.并删除当前在线日志文件 我的測试 ...
- 一起talk C栗子吧(第九十 三回:C语言实例--进程间通信之临界资源)
各位看官们.大家好,前面章回中咱们说的是使用信号和管道进行进程间通信的样例.这一回咱们说的样例是:进程间通信之临界资源.闲话休提,言归正转.让我们一起talk C栗子吧! 我们首先介绍一下,什么是临界 ...
- jquery中$(dom).each()和$(dom).length的使用
1.$(dom).each();在dom处理上用的比较多. $(selector).each(function(index,element){ //selector会遍历当前页面里所有匹配的jquer ...
- Oracle学习总结(10)——45 个非常有用的 Oracle 查询语句
****************************** 日期/时间 相关查询 ***************************** -- 1.获取当前月份的第一天 sele ...
- mybatis 嵌套查询子查询column传多个参数描述
https://my.oschina.net/softwarechina/blog/375762
- 三星Galaxy Tab S2上市,压制苹果之心凸显
平板市场正在迎来史上最为关键的一次PK,众所周知,平板市场的苹果和三星一直是行业的领头羊,而在激烈的竞争中.三星平板似乎后劲更足.众多性能优异的产品频频推出.平板之王的称谓呼之欲出. 去年三星 ...
- ds1302模块的一个arduino程序
/* * 读写DS1302 时钟芯片 * @author Yangtf * 很棒的文档 http://www.21ic.com/jichuzhishi/datasheet/DS1302/data/18 ...
- ZOJ 2421 Recaman's Sequence
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1421 题目大意: 定义a^m为 a^m = a^(m-1) - m 如果a^ ...