码迷,mamicode.com
首页 > 其他好文 > 详细

[React] Implement a Higher Order Component with Render Props

时间:2017-12-19 01:14:46      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:most   static   find   reac   post   ...   ext   cti   ike   

When making a reusable component, you‘ll find that people often like to have the API they‘re most familiar with, so in this lesson we‘ll recreate the withToggle Higher Order Component using our new ConnectedToggle render prop component.

 

function withToggle(Component) {
  function Wrapper(props, context) {
    const {innerRef, ...remainingProps} = props
    return (
      <ConnectedToggle
        render={toggle => (
          <Component
            {...remainingProps}
            toggle={toggle}
            ref={innerRef}
          />
        )}
      />
    )
  }
  Wrapper.displayName = `withToggle(${Component.displayName ||
    Component.name})`
  Wrapper.propTypes = {innerRef: PropTypes.func}
  Wrapper.WrappedComponent = Component
  return hoistNonReactStatics(Wrapper, Component)
}

 

 

[React] Implement a Higher Order Component with Render Props

标签:most   static   find   reac   post   ...   ext   cti   ike   

原文地址:http://www.cnblogs.com/Answer1215/p/8059281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!