Skip to content

Instantly share code, notes, and snippets.

@slightlytyler
Last active September 20, 2016 20:50
Show Gist options
  • Save slightlytyler/0515955d30b35d8e18c6240047924805 to your computer and use it in GitHub Desktop.
Save slightlytyler/0515955d30b35d8e18c6240047924805 to your computer and use it in GitHub Desktop.
An example of a UserDetail component
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