Skip to content

Instantly share code, notes, and snippets.

@gpbmike
Last active December 11, 2016 17:47
Show Gist options
  • Save gpbmike/8c870b1925acb6ff8fe4786528b447cf to your computer and use it in GitHub Desktop.
Save gpbmike/8c870b1925acb6ff8fe4786528b447cf to your computer and use it in GitHub Desktop.
import React from 'react';
class FetchUser extends React.Component {
state = {
user: undefined,
}
componentDidMount() {
if (this.props.userid) {
this.fetchUser(this.props.userid);
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.userid && this.props.userid !== nextProps.userid) {
this.fetchUser(nextProps.userid);
}
}
fetchUser = userid => APIFetchUser(userid).then(user => this.setState({ user }));
render() {
this.props.children(this.state.user);
}
}
export default FetchUser;
import React from 'react';
const UserRoute = ({ params: { userid } }) => (
<FetchUser userid={userid}>
{(user) => (user ? <User user={user} /> : <SomethingElse />)}
</FetchUser>
);
export default UserRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment