zoukankan      html  css  js  c++  java
  • [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;
  • 相关阅读:
    Go基础
    格式化输入输出
    常量
    Go语言基础之变量
    跨平台编译
    Hello World
    使用go module导入本地包
    Go语言之依赖管理
    Go包管理
    Go项目结构
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6603166.html
Copyright © 2011-2022 走看看