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.

[React] React Router: setRouteWillLeaveHook的更多相关文章

  1. ReactJS React+Redux+Router+antDesign通用高效率开发模板,夜间模式为例

    工作比较忙,一直没有时间总结下最近学习的一些东西,为了方便前端开发,我使用React+Redux+Router+antDesign总结了一个通用的模板,这个技术栈在前端开发者中是非常常见的. 总的来说 ...

  2. Nginx支持 React browser router

    修改nginx配置文件,添加try_file配置如下,即可实现对 React browser router 的支持. location / { root /var/www/mysite; try_fi ...

  3. React+React Router+React-Transition-Group实现页面左右滑动+滚动位置记忆

    2018年12月17日更新: 修复在qq浏览器下执行pop跳转时页面错位问题 本文的代码已封装为npm包发布:react-slide-animation-router 在React Router中,想 ...

  4. react 装 router - yarn add react-router-dom@next

    react 装 router yarn add react-router-dom@next

  5. [React] react+redux+router+webpack+antd环境搭建一版

    好久之前搭建的一个react执行环境,受历史影响是webpack3.10.0和webpack-dev-server2.7.1的环境,新项目准备用webpack4重新弄弄了,旧的记录就合并发布了(在没有 ...

  6. react react使用css

    在react 中使用css有以下几种方法 第一种全局使用 app.js import React from 'react'; import Router from "./router&quo ...

  7. React/React Native 的ES5 ES6写法对照表

    //es6与es5的区别很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component ...

  8. React/React Native 的ES5 ES6写法对照表-b

    很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends React.Component),然而网上搜到的很多教 ...

  9. [React] React Fundamentals: Integrating Components with D3 and AngularJS

    Since React is only interested in the V (view) of MVC, it plays well with other toolkits and framewo ...

  10. React: React组件的生命周期

    一.简介 在前面的第二篇博文中对组件的生命周期虽然做了一个大略介绍,但总感觉说的过于简单,毕竟生命周期是React组件的核心部分.在我们熟练使用React挂载和合成组件来创建应用表现层的过程中,针对数 ...

随机推荐

  1. Visual Studio2015 Community一些必备插件

    Visual Studio2015 Community一些必备插件 是不是感觉虽然VS2015的代码编辑能力已经很强大了,但是总感觉差了那么一些呢?不用担心,它有很多非常强大的插件,能够让你打代码事半 ...

  2. nginx添加缓存

    nginx的具体逻辑是什么样的? 分布式session spring session redis过滤器 有4种方案: 一直访问一台 //如果这台机器垮掉了,怎么办? session同步 序列化传输 / ...

  3. mysql慢查优化总结

    1.优化sql语句结构 or改成union,使用start,limit 先只查询出所有的id,然后再排序.如果查询出所有的id仍然很慢,就要仔细考虑了. 2.添加索引 mysql每次查询只能使用一个索 ...

  4. EF中使用Contains方法

    第一种情况 var db=new ECEntities(); var list=new []{"8","9"}; var result=from a in db ...

  5. CodeIgniter目录结构

    1.1   application 是你自己的项目存放文件的目录(控制器.模型和视图等!) (1)分析application文件夹中的目录 (1.1)         cache文件是放缓存文件 (1 ...

  6. ORACLE外键和锁

    在oracle中,如果外键未加索引,对父表的修改,会导致子表被加上全表锁.这包括两种情况: 1.删除父表中的行,如果外键上没有索引,会导致子表被加上全表锁 2.更新父表的主键(根据关系数据库的原则,更 ...

  7. 《Linux内核分析》 week8作业-Linux加载和启动一个可执行程序

    一.ELF文件格式 ELF(Executable and Linking Format)是x86 Linux系统下常用的目标文件格式,有三种主要类型: 适于连接的可重定位文件,可与其他目标文件一起创建 ...

  8. geoserver + postgis+postgresql+agslib.swc

    运用开源的geoserver+postgis+postgresql+arcgis for flex api 开发地图应用系统. 1.Geoserver GeoServer 是 OpenGIS Web ...

  9. 2016最新一元云购完整源码 云购CMS系统 带安卓和ios手机客户端 源码免费分享

    原文转自:http://www.zccode.com/thread-724-1-1.html 该资源说明: 看起来不错的一套一元云购CMS源码,源码包里面带了安卓和ios手机客户端,手机客户端需要自己 ...

  10. 一个读取C#特性Description方法

    class Program { static void Main(string[] args) { string str= DB.write.ToDescription(); Console.Writ ...