Created
January 23, 2019 14:50
-
-
Save jordanrios94/5f83f3c40fad76552ff3e3fa7b1078c2 to your computer and use it in GitHub Desktop.
HOC Scaffold Redux + React Router DOM
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 } 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)) |
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 } 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