zoukankan      html  css  js  c++  java
  • [React] React Router: setRouteWillLeaveHook

    setRouteWillLeaveHook provides a method for us to intercept a route change before leaving the current route.

    Route Hook with Context to works with, So if we want to handle the route hook for Home Componet, we nee to add Context for Home Component:
    class Home extends React.Component{
    
        componentWillMount(){
            this.context.router.setRouteLeaveHook(
                this.props.route,
                this.routeWillLeave
            )
        }
    
        routeWillLeave(nextLocation){
            console.log(nextLocation);
            return `Leave for next Location ${nextLocation.pathname}`;
        }
    
        render(){
            return <div><h1>Home</h1><Links></Links></div>
        }
    }
    Home.contextTypes = {
        router: React.PropTypes.object.isRequired
    };

    routeWillLeave is custom function, you can call it whatever you want, inside function, you can do the stuff you want, if you return a string, the string will show in alert dialog.

  • 相关阅读:
    saltstack笔记
    saltstack学习篇
    tomcat学习篇
    memcache笔记
    sed、awk学习篇
    javascript function call
    jvm性能调优工具
    spring boot 单元测试
    activemq 分布式事务
    sringboot 集成dobbo2.5.3
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5341153.html
Copyright © 2011-2022 走看看