Posted By


dylansong on 06/08/16

Tagged


Statistics


Viewed 168 times
Favorited by 0 user(s)

10.routerWillLeave


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import React from 'react';
  2. import { Router, Route, Link, hashHistory } from 'react-router';
  3.  
  4. // const Home = () => <div><h1>Home</h1><Links /></div>;
  5.  
  6. class Home extends React.Component {
  7. componentWillMount(){
  8. this.context.router.setRouteLeaveHook(
  9. this.props.route,
  10. this.routerWillLeave
  11. )
  12. }
  13.  
  14. routerWillLeave( nextLocation ){
  15. return `leaving home for ${nextLocation.pathname}`
  16. }
  17.  
  18. render(){
  19. return <div><h1>Home</h1><Links /></div>;
  20. }
  21. }
  22.  
  23. Home.contextTypes = { router: React.PropTypes.object.isRequired }
  24.  
  25. const About = () => <div><h1>About</h1><Links /></div>;
  26. const Links = () => {
  27. return (
  28. <nav>
  29. <Link to="/">Home</Link>
  30. <Link to="about">About</Link>
  31. </nav>
  32. )
  33. };
  34.  
  35. const App = () => {
  36. return (
  37. <Router history={ hashHistory }>
  38. <Route path="/" component={Home}></Route>
  39. <Route path="/about" component={About}></Route>
  40. </Router>
  41. )
  42. };
  43.  
  44. export default App;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.