Skip to content

Instantly share code, notes, and snippets.

@guilleasz
Created January 26, 2018 15:40
Show Gist options
  • Save guilleasz/f6155151a8df304cf010b02fb8ca20fa to your computer and use it in GitHub Desktop.
Save guilleasz/f6155151a8df304cf010b02fb8ca20fa to your computer and use it in GitHub Desktop.
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