Last active
March 16, 2018 19:07
-
-
Save johnloven/20279e45e905790214d96a7f31a75f34 to your computer and use it in GitHub Desktop.
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 App extends React.Component { | |
render() { | |
return ( | |
<div> | |
<StudentPage/> | |
<OtherPage/> | |
<AndOtherPage}/> | |
<div> | |
); | |
} | |
} | |
const fetchData = urlCreator => WrappedComponent => | |
class DataContainer extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { data: null }; | |
} | |
componentDidMount() { | |
const url = urlCreator(); | |
fetch(url) | |
.then(response => response.json()) | |
.then(json => this.setState({ data: json })) | |
} | |
render() { | |
return <WrappedComponent data={this.state.data}/> | |
} | |
} | |
@fetchData(() => "/myschool/students") | |
class StudentPage extends React.Component { | |
render() { | |
return ( | |
<div> | |
{this.props.data.map(student => <div>student.name</div>)} | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment