Last active
April 13, 2019 17:39
-
-
Save Tahseenm/c5bff992601dca52775adaa4c8d037b1 to your computer and use it in GitHub Desktop.
Hooks vs no hooks
This file contains 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 from 'react' | |
import * as _ from 'lodash' | |
/*---------------------------------------------------------------------------*\ | |
USER | |
\*---------------------------------------------------------------------------*/ | |
const UserContext = React.createContext() | |
const UserProvider = withRouter(class UserProvider extends React.Component { | |
constructor(...args) { | |
this.state = { | |
user: null | |
} | |
} | |
login(email, password) { | |
try { | |
const { data: user } = api.post('/login', { emaiil, password }) | |
this.setState({ user }) | |
this.props.history.push('/dashboard') | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
logout() { | |
this.setState({ user: null }) | |
this.props.history.push('/sign-in') | |
} | |
render() { | |
const value = { | |
user: this.state.user, | |
login: this.login, | |
logout: this.logout | |
} | |
return ( | |
<UserContext.Provider value={value}> | |
{children} | |
</UserContext.Provider> | |
) | |
} | |
}) | |
/*---------------------------------------------------------------------------*\ | |
APP | |
\*---------------------------------------------------------------------------*/ | |
class App extends React.Component { | |
static contextType = UserContext | |
render() { | |
const { user } = this.context | |
if (_.isNull(user)) { | |
return <Redirect to="/sign-in" /> | |
} | |
return <Dashboard user={user} /> | |
} | |
} |
This file contains 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 from 'react' | |
import * as _ from 'lodash' | |
/*---------------------------------------------------------------------------*\ | |
USER | |
\*---------------------------------------------------------------------------*/ | |
const UserContext = React.createContext() | |
const UserProvider = ({ children }) => { | |
const { history } = useRouter() | |
const [user, setUser] = React.useState(null) | |
const login = async (email, password) => { | |
try { | |
const { data: user } = api.post('/login', { emaiil, password }) | |
setUser(user) | |
history.push('/dashboard') | |
} catch (e) { | |
console.error(e) | |
} | |
} | |
const logout = () => { | |
setUser(null) | |
history.push('/sign-in') | |
} | |
const value = { user, login, logout } | |
return ( | |
<UserContext.Provider value={value}> | |
{children} | |
</UserContext.Provider> | |
) | |
} | |
/*---------------------------------------------------------------------------*\ | |
APP | |
\*---------------------------------------------------------------------------*/ | |
const App = () => { | |
const { user } = React.useContext(UserContext) | |
if (_.isNull(user)) { | |
return <Redirect to="/sign-in" /> | |
} | |
return <Dashboard user={user} /> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment