Last active
September 20, 2016 20:50
-
-
Save slightlytyler/0515955d30b35d8e18c6240047924805 to your computer and use it in GitHub Desktop.
An example of a UserDetail component
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
import React, { Component, PropTypes } from 'react' | |
export class UserDetail extends Component { | |
static propTypes = { | |
fetchUser: PropTypes.func.isRequired, | |
record: PropTypes.object, | |
} | |
componentWillMount() { | |
if (!this.props.record) this.props.fetchUser(); | |
} | |
render() { | |
if (!this.props.record) { | |
return <div>Loading...</div>; | |
} | |
return <div>Current user is {this.props.user.name}.</div>; | |
} | |
} | |
import { connect } from 'react-redux'; | |
import { bindActionCreators } from 'redux'; | |
import { fetchUser } from 'modules/users/actions'; | |
import { getUserById } from 'modules/users/selectors'; | |
const getUserId = props => props.params.userId; | |
export default connect( | |
(state, props) => ({ | |
record: getUserById(state, getUserId(props)), | |
}), | |
(dispatch, props) => () => bindActionCreators({ | |
fetchUser: () => fetchUser(getUserId(props)), | |
}, dispatch) | |
)(UserDetail) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment