技术控

    今日:51| 主题:49312
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] React Component Curry

[复制链接]
反腐 发表于 2016-10-5 15:11:58
130 1

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Everyone loves curry don't they? I don't know about you but I'm going for one on Friday.
  When React 0.14 shipped, it came with a new way to write React components. Rather than as an ES2015 class or using    React.createClassthere was now another way: stateless functional components.  
  These are components which have no state (the name gives it away) and a simple syntax; they are a function which takes your component props as a single parameter and they return JSX. Think of them as the render method of a standard component just with props as a parameter.
  The advantage of these components is that they can reduce the amount of code you have to write for a component which requires no state. This is even more true if you're using ES2015 syntax as you have arrow functions and destructuring to help.Embrace the terseness!
  Mine's a Balti

  There is another advantage of this syntax. If you have a number of components which share similar implementation you can easily make component factories by currying:
  1. function iconMaker(fontAwesomeClassName: string) {
  2.    return props => <i className={ `fa ${ fontAwesomeClassName }` }/>;
  3. }
  4. const ThumbsUpIcon = iconMaker("fa-thumbs-up");
  5. const TrophyIcon = iconMaker("fa-trophy");
  6. // Somewhere in else inside a render function:
  7. <p>This is totally <ThumbsUpIcon />.... You should win a <TrophyIcon /></p>
复制代码
So our    iconMakeris a function which, when called with a    Font Awesomeclass name produces a function which, when invoked, will return a the HTML required to render that icon. This is a super simple example, a bhaji if you will, but you can imagine how useful this technique can be when you've more of a banquet in mind.
友荐云推荐




上一篇:在 Android 中使用 Java8 的特性
下一篇:Are RethinkDB and Horizon abandoned?
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

1263481627 发表于 2016-10-5 17:15:34
爱情就像两个拉着橡皮筋的人,受伤的总是不愿意放手的那一个!
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表