-
-
Save minmaxdata/1e704023f43424968d09bb8b8b8cee96 to your computer and use it in GitHub Desktop.
Higher Order Component (HOC) example with React
This file contains 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
// Higher Order Component (HOC) : A component that renders another component | |
// Goal: Reduce code, render hijacking, prop manipulation, abstract state | |
// Inspired by mead.io videos on Udemy | |
import React from 'react' | |
import reactDOM from 'react-dom' | |
const info = (props) => ( | |
<div> | |
<h1>Hello { props.isAdmin ? 'admin' : 'guest' }</h1> | |
<p>Info: { props.info }</p> | |
</div> | |
) | |
const withWelcomeMessage = (WrappedComponent) => { | |
return (props) => ( | |
<div> | |
{ props.isAdmin && <p>This is private info, please do not share!</p> } | |
<WrappedComponent { ...props } /> | |
</div> | |
) | |
} | |
const UserInfo = withWelcomeMessage(info) | |
reactDOM.render(<UserInfo isAdmin={true} info="info detailes passed" />, document.getElementById('app')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment