Skip to content

Instantly share code, notes, and snippets.

@johnloven
Last active March 16, 2018 19:07
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 johnloven/20279e45e905790214d96a7f31a75f34 to your computer and use it in GitHub Desktop.
Save johnloven/20279e45e905790214d96a7f31a75f34 to your computer and use it in GitHub Desktop.
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