Created
January 26, 2018 15:40
-
-
Save guilleasz/f6155151a8df304cf010b02fb8ca20fa to your computer and use it in GitHub Desktop.
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 RouteHook from 'react-route-hook'; | |
const Data = (props) => ( | |
<div> | |
{props.data} | |
</div> | |
); | |
class AppContainer extends React.Component { | |
state = { | |
data: '', | |
} | |
fetchData = (props) => { | |
axios.get(`/api/${props.match.params.id}`) | |
.then(res => this.setState({ data: res.data })) | |
} | |
shouldFetchData = (newProps, oldProps) => { | |
if (oldProps.match.params.id !== newProps.match.params.id) { | |
this.fetchData(newProps) | |
} | |
} | |
logout(props) { | |
console.log(`You are leaving the route ${props.location.pathname}`) | |
} | |
render() { | |
return ( | |
<RouteHook | |
path="home/:id" | |
onEnter={this.fetchData} | |
onChange={shouldFetchData} | |
onLeave={this.logout} | |
render={(routerProps) => <Data data={this.state.data} />} | |
/> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment