[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
withToggleHigher Order Component using our new
ConnectedTogglerender 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] Cleanly Map Over A Stateless Functional Component with a Higher Order Component
- [Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose
- React 语法一:React.createClass()、ReactDOM.render()、props、state和componentDidMount()
- Unable to resolve module `react/lib/ReactComponentWithPureRenderMixin`
- [React] Use Prop Collections with Render Props
- [React] Validate Custom React Component Props with PropTypes
- [React Intl] Use a react-intl Higher Order Component to format messages
- [React] Stop Memory Leaks with componentWillUnmount Lifecycle Method in React
- react native ios添加ART.xcodeproj(ReactNative No component found for view with name “ARTSurfaceView”)
- [React Intl] Render Content with Placeholders using react-intl FormattedMessage
- React Native开发坑之null is not an object (evaluating 'inst.componentWillReceiveProps')
- [React] Make Controlled React Components with Control Props
- [React Intl] Render Content with Markup Using react-intl FormattedHTMLMessage
- Reactjs Component 渲染(Render)机制
- [React] Update Component State in React With Ramda Lenses
- [Recompose] Make Reusable React Props Streams with Lenses
- [React] Define defaultProps and PropTypes as static methods in class component
- 通过componentWillReceiveProps()实现React左右父子页面的联动效果
- React Render Props 模式
- [RxJS] Flatten a higher order observable with mergeAll in RxJS