Skip to content

Instantly share code, notes, and snippets.

@st8998
Created March 28, 2016 21:59
Show Gist options
  • Save st8998/3a0fd06428fd67d427c3 to your computer and use it in GitHub Desktop.
Save st8998/3a0fd06428fd67d427c3 to your computer and use it in GitHub Desktop.
@connect(
(state, props) => ({ user: find(propEq('id', Number(props.params.id)), state.users) }),
{ requestUser }
)
export default class UserProfile extends Component {
static contextTypes = {
router: React.PropTypes.object
};
componentWillMount() {
// come from another page to /profile/:id
if (!this.props.user) this.props.requestUser(this.props.params.id)
.catch(() => this.context.router.push('/notfound'))
}
componentWillUpdate(nextProps) {
// change id param of the route /profile/1 -> /profile/122
if (!nextProps.user)
this.props.requestUser(nextProps.params.id)
.catch(() => this.context.router.push('/notfound'))
}
render() {
return <Profile user={this.props.user}></Profile>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment