您的位置:首页 > Web前端 > React

[React] Implement a Higher Order Component with Render Props

2017-12-18 19:50 489 查看
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)
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: