[React Router v4] Intercept Route Changes
If a user has entered some input, or the current Route is in a “dirty” state and we want to confirm that data will be lost, React Router v4 provides a Prompt component to interrupt the Route transition and ask the user a question.
For example we need to way to tell user if he leaves the page, data will lost.
We can use 'Prompt' component from router lib.
import React from 'react';
import {Link, Route, Prompt} from 'react-router-dom'; const Home = () => (<h1>Home</h1>);
class Form extends React.Component {
state = {
dirty: false
}; setDirty = () => {
this.setState((preState, props) => {
return {
dirty: true
}
})
}; render(){
return(
<div>
<h1>Form</h1>
<input type="text" onInput={this.setDirty}/>
<Prompt
when={this.state.dirty}
message="Date will be lost"
></Prompt>
</div>
);
}
} const Guards = () => (
<div>
<Link to="/guards/home">Home</Link>
<Link to="/guards/form">Form</Link> <Route path="/guards/home" component={Home}></Route>
<Route path="/guards/form"
component={Form}
></Route>
</div>
); export default Guards;
[React Router v4] Intercept Route Changes的更多相关文章
- [React Router v4] Conditionally Render a Route with the Switch Component
We often want to render a Route conditionally within our application. In React Router v4, the Route ...
- [React Router v4] Render Multiple Components for the Same Route
React Router v4 allows us to render Routes as components wherever we like in our components. This ca ...
- [Web 前端] React Router v4 入坑指南
cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...
- React Router V4发布
React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...
- [React Router v4] Redirect to Another Page
Overriding a browser's current location without breaking the back button or causing an infinite redi ...
- [React Router v4] Render Catch-All Routes with the Switch Component
There are many cases where we will need a catch-all route in our web applications. This can include ...
- [React Router v4] Render Nested Routes
With React Router v4 the entire library is built as a series of React components. That means that cr ...
- [React Router v4] Parse Query Parameters
React Router v4 ignores query parameters entirely. That means that it is up to you to parse them so ...
- [React Router v4] Use Regular Expressions with Routes
We can use regular expressions to more precisely define the paths to our routes in React Router v4. ...
随机推荐
- COGS——T 2057. [ZLXOI2015]殉国
http://cogs.pro/cogs/problem/problem.php?pid=2057 ★☆ 输入文件:BlackHawk.in 输出文件:BlackHawk.out 评测插件 ...
- 浅谈Git与SVN的使用感受
作为版本号控制工作.两者的做大的差别应该在于:Git属于分布式版本号控制工具,而SVN属于集中式的版本号控制工具.分布式的优点是什么呢?举个样例来说.当你在火车上离线状态下编程工作,在某个阶段会须要先 ...
- System.out.println 的多线程并发问题
假设println函数的參数为常量则不会出现线程并发问题,可是假设參数为表达式形式.则JVM在运行println函数的时候会分为几步来运行,从而造成并发问题. 例如以下样例所看到的: package ...
- jsonp跨域实例丨利用百度数据制作搜索页面
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- 学习笔记:Vue——动态组件&异步组件
动态组件 01.在动态组件上使用keep-alive,保持组件的状态,以避免反复重渲染导致的性能问题. <!-- 失活的组件将会被缓存!--> <keep-alive> < ...
- postman带cookie进行请求
接口地址: https://m.xxxx.com/api/front/activity/xs/session 打开postman的headers, 然后复制fiddler中接口的cookie,设置co ...
- iOS开发之CocoaPods(objective-c第三方库管理工具)
介绍: iOS开发中,大多数情况下,我们都须要集成一些第三方依赖库.对于一个稍大的项目,用到的第三方依赖库的数量也很可观.CocoaPods是objective-c第三方库管理工具,方便第三方库的管理 ...
- 读文件头数据判断 PE 文件格式和类型
namespace X.Reflection { using System; using System.IO; public static partial class ReflectionX { pu ...
- BigQuery分析GitHub上的C#
BigQuery分析GitHub上的C# 一年多以前,Google 在GitHub中提供了BigQuery用于查询的GitHub上的开源代码(open source code on GitHub av ...
- [arm]虚拟机,2440开发板,主机三者互通
想实现3着互通先必须保证三者的网段是相同的: 首先查看电脑主机的IP: 然后再看看虚拟机的IP: 惊喜的发现,他们在一个网段上---那就不用改了-- 再去看看开发板上的IP: 这里注意,输入命令时,是 ...