Last active
December 11, 2016 17:47
-
-
Save gpbmike/8c870b1925acb6ff8fe4786528b447cf to your computer and use it in GitHub Desktop.
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'; | |
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; |
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'; | |
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