This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render() { | |
// Filter out extra props and don’t pass through | |
const { extraProp, ...passThroughProps } = this.props; | |
// Inject props into the wrapped component. | |
// Usually the value of state or an instance method. | |
const injectedProp = someStateOrInstanceMethod; | |
// Pass props to the Wrapped component | |
return ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function logProps(WrappedComponent) { | |
return class extends React.Component { | |
componentDidUpdate(prevProps) { | |
console.log("Current props: ", this.props); | |
console.log("Previous props: ", prevProps); | |
} | |
render() { | |
// Wrap the input component in a container without modifying it, Nice! | |
return <WrappedComponent {...this.props} />; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function logProps(InputComponent) { | |
InputComponent.prototype.componentDidUpdate = function(prevProps) { | |
console.log("Current props: ", this.props); | |
console.log("Previous props: ", prevProps); | |
}; | |
// Return the original input component, which has been modified. | |
return InputComponent; | |
} | |
// Every time logProps is called, the enhanced component will have log output. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HOCFactoryFactory = (params) => { | |
// Operation params here | |
return (WrappedComponent) => { | |
return class EnhancedComponent extends WrappedComponent { | |
render() { | |
return params.isRender && this.props.isRender && super.render(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HOC = (WrappedComponent) => { | |
return class EnhancedComponent extends WrappedComponent { | |
componentDidMount(){ | |
// ... | |
} | |
render() { | |
return super.render(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HOC = (WrappedComponent) => { | |
return class EnhancedComponent extends WrappedComponent { | |
render() { | |
return this.props.isRender && super.render(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HOC = (WrappedComponent) => { | |
return class EnhancedComponent extends React.Component { | |
render() { | |
return ( | |
<div class="layout"> | |
<WrappedComponent {...this.props} /> | |
</div> | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WrappedComponent extends React.Component { | |
render() { | |
return <input name="name" />; | |
} | |
} | |
const HOC = (WrappedComponent) => { | |
return class EnhancedComponent extends React.Component { | |
constructor(props) { | |
super(props); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HOC = (WrappedComponent, store) => { | |
return class EnhancedComponent extends React.Component { | |
render() { | |
const newProps = { | |
id: store.id | |
} | |
return <WrappedComponent | |
{...this.props} | |
{...newProps} | |
/>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// High-order component definition | |
const higherOrderComponent = (WrappedComponent) => { | |
return class EnhancedComponent extends React.Component { | |
// ... | |
render() { | |
return <WrappedComponent {...this.props} />; | |
} | |
}; | |
} |