function control(wrappedComponent) { return class Control extends React.Component { render(){ let props = { ...this.props, message: "You are under control" }; return <wrappedComponent {...props} /> } } }
functionenhance(WrappedComponent) { classEnhanceextendsReact.Component {/*...*/} // Must know exactly which method(s) to copy :( Enhance.staticMethod = WrappedComponent.staticMethod; returnEnhance; }
例子
logger和debugger
官网的例子,可以用来监控父级组件传入的props的改变:
1 2 3 4 5 6 7 8 9 10 11 12
function logProps(WrappedComponent) { return class extends React.Component { componentWillReceiveProps(nextProps) { console.log(`WrappedComponent: ${WrappedComponent.displayName}, Current props: `, this.props); console.log(`WrappedComponent: ${WrappedComponent.displayName}, Next props: `, nextProps); } render() { // Wraps the input component in a container, without mutating it. Good! return <WrappedComponent {...this.props} />; } } }