Skip to content

Instantly share code, notes, and snippets.

@jordanrios94
Created January 23, 2019 14:50
Show Gist options
  • Save jordanrios94/5f83f3c40fad76552ff3e3fa7b1078c2 to your computer and use it in GitHub Desktop.
Save jordanrios94/5f83f3c40fad76552ff3e3fa7b1078c2 to your computer and use it in GitHub Desktop.
HOC Scaffold Redux + React Router DOM
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchAdmins } from '../actions';
import requireAuth from '../components/hocs/requireAuth';
class AdminsListPage extends Component {
componentDidMount() {
this.props.fetchAdmins();
}
renderAdmins() {
return this.props.admins.map(admin => (
<li key={admin.id}>{admin.name}</li>
));
}
render() {
return (
<div>
<h3>Protected list of admins</h3>
<ul>{this.renderAdmins()}</ul>
</div>
);
}
}
const mapStateToProps = ({ admins }) => {
return { admins };
};
export default connect(mapStateToProps, { fetchAdmins })(requireAuth(AdminsListPage))
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom';
export default ChildComponent => {
class RequireAuth extends Component {
render() {
switch (this.props.auth) {
case false:
return <Redirect to="/" />
case null:
return <div>Loading...</div>;
default:
return <ChildComponent {...this.props} />
}
}
}
const mapStateToProps = ({ auth }) => {
return { auth };
};
return connect(mapStateToProps)(RequireAuth);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment