[React] Implement a Higher Order Component with Render Props

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[React] Implement a Higher Order Component with Render Props相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Implement strStr()

28. Implement strStr()

React 18发布,仅用400行代码就能实现一个Mini-React

28. Implement strStr()

Implement a customized pin code input control

LeetCode - 28. Implement strStr()