Last active
April 28, 2018 19:16
-
-
Save aquiseb/2ff949f0c5bd5aa37f116727c87cbbcf to your computer and use it in GitHub Desktop.
Reactjs Nextjs HOC
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
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 |
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
import React from 'react' | |
import { Enhance } from "../components/Enhance"; | |
export default Enhance((props) => { | |
console.log(props) | |
return ( | |
<div>{props.data}</div> | |
) | |
}) |
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
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