Skip to content

Instantly share code, notes, and snippets.

@kcak11
Last active April 2, 2020 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcak11/49995ae636b191a16621738d07172c5f to your computer and use it in GitHub Desktop.
Save kcak11/49995ae636b191a16621738d07172c5f to your computer and use it in GitHub Desktop.
A Simple HOC

A Simple HOC

(for React)

<div id="app"></div>
const { Fragment } = React;
const MyHOC = function (InputComponent) {
let myCustomProps = {
style: {
color: "red",
"font-family": "Verdana",
"font-size": "20px"
},
onClick: function () {
alert("You clicked this button");
}
};
return function (props) {
return <InputComponent {...props} {...myCustomProps} />;
};
};
const App = function () {
let MyButton = MyHOC("button");
return (
<Fragment>
<span>A Simple Button</span>
<br />
<button>click me</button>
<br /><br/>
<span>A HOC Button</span>
<br />
<MyButton>click me</MyButton>
</Fragment>
);
};
ReactDOM.render(<App />, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment