Skip to content

Instantly share code, notes, and snippets.

@aquiseb
Last active April 28, 2018 19:16
Show Gist options
  • Save aquiseb/2ff949f0c5bd5aa37f116727c87cbbcf to your computer and use it in GitHub Desktop.
Save aquiseb/2ff949f0c5bd5aa37f116727c87cbbcf to your computer and use it in GitHub Desktop.
Reactjs Nextjs HOC
import React from 'react'
import { Enhance } from "../components/Enhance";
class MyComponent extends React.Component {
render() {
if (!this.props.data) return <div>Waiting...</div>;
return <div>{this.props.data}</div>;
}
}
export default Enhance(MyComponent); // Enhanced component
import React from 'react'
import { Enhance } from "../components/Enhance";
export default Enhance((props) => {
console.log(props)
return (
<div>{props.data}</div>
)
})
import { Component } from "react";
export var Enhance = ComposedComponent => class extends Component {
constructor() {
super();
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
return <ComposedComponent {...this.props} data={this.state.data} />;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment