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