技术控

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

[其他] React Component Curry

[复制链接]
反腐 发表于 2016-10-5 15:11:58
154 1
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.
1263481627 发表于 2016-10-5 17:15:34
爱情就像两个拉着橡皮筋的人,受伤的总是不愿意放手的那一个!
回复 支持 反对

使用道具 举报

我要投稿

回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表