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. 最近很火的携程Java 工程师的一道面向对象面试题

    最近这道面试题,传遍程序员各大小园地,本小白特摘抄整理与大家一起学习: 原题: package com.gxlee; public class Base { private String baseNa ...

  2. Android Design Support Library: 学习CoordinatorLayout

    简述 CoordinatorLayout字面意思是"协调器布局",它是Design Support Library中提供的一个超级帧布局,帮助我们实现Material Design ...

  3. JNI类型与C/C++映射关系

    Java 类型 本地类型 描述 boolean jboolean C/C++8位整型 byte jbyte C/C++带符号的8位整型 char jchar C/C++无符号的16位整型 short ...

  4. colorful-记录好看的颜色

    p { float: left; width: 100px; height: 100px; border: 1px solid black; margin: 5px; text-align: cent ...

  5. 【JQuery学习笔记】一、基础篇

    Lesson2 Ready 和onload类似,但onload只能注册一次,后注册的取代现注册的,而Ready可以多次注册都会执行 onload是所有院所创建完毕.图片.Css等都加载完毕后才触发,而 ...

  6. C程序第二章节:算法

    1.主要讲了:算法,3种基本结构化的算法(顺序,选择,循环结构),N-S流程图表示算法,伪代码表示算法. 2.输入10个数,输出其中最大的一个数. #include <stdio.h>in ...

  7. 1.Tomcat配置

    1.启动 解压缩安装包后,点击startup.bat,保持控制台窗口开启 浏览器中输入http://localhost:8080 后看到启动界面则表示启动成功 点击shutdown.bat则关闭Tom ...

  8. css画小米、遨游logo

    狠简单的2个Logo,用纯css写出来,觉得挺好玩的. <!DOCTYPE html> <html> <head> <meta charset="u ...

  9. Swift语法之 ---- ?和!区别

    1.常量和变量 Swift语言中是用let来定义常量,并且要初始化.var来定义变量,在let或者var后面申明类型,冒号+空格,然后再加上类型名称. 2.optional(可选)变量 可选变量用于处 ...

  10. 通知栏快捷按钮自定义教程以及快捷面板提取的思路-转自魔趣论坛-lonyii2

    原帖地址: http://bbs.mfunz.com/forum.php?mod=viewthread&tid=235198&extra=page%3D1%26filter%3Dtyp ...