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;

最新文章

  1. ubuntu14.04下安装node.js
  2. DAO层,Service层,Controller层、View层 的分工合作
  3. socket 通信
  4. 阅读《LEARNING HARD C#学习笔记》知识点总结与摘要一
  5. Laravel 5.1 文档攻略 —— Eloquent:模型关系
  6. oo智慧
  7. (转载)ubuntu安装pyton-pip问题解决
  8. php信用卡卡号验证函数
  9. ExtJs4 笔记(10) Ext.tab.Panel 选项卡
  10. 文章之间的基本总结:Activity生命周期
  11. RabbitMQ-Spring AMQP
  12. 【一天一道LeetCode】#345. Reverse Vowels of a String
  13. (十一)QPainter绘图, QPixmap,QImage,QPicture,QBitmap
  14. python将多个pdf合成一个
  15. django 源码报错
  16. Python中的短路计算
  17. flask微服务框架的初步接触
  18. 学习web components
  19. C++ 使用system()实现锁定屏幕
  20. delphi 中如何处理“幽灵”、“熔断”?(转载)

热门文章

  1. iTOP-4412开发板使用
  2. C# 异步延时执行
  3. JSON.parse和eval()的区别
  4. Vue的学习--开始之前的思考
  5. position记录
  6. BZOJ3262: 陌上花开(三维偏序,CDQ分治)
  7. ArcGIS Engine 线段绘制
  8. Spring Profiles example--转载
  9. JS错误记录 - 按左右箭头div移动、一串div跟着鼠标移动
  10. jmeter--函数助手对话框之参数详解