• [React Router] Prevent Navigation with the React Router Prompt Component


    In this lesson we'll show how to setup the Prompt component from React Router. We'll prompt with a static message, as well as a dynamic method with the message as a function. Finally we'll show that you can return true from the message as a function to dynamically allow navigation.

    import React, { Component } from "react";
    import { Prompt } from "react-router-dom";
    
    class Profile extends Component {
      state = {
        name: "",
      };
      render() {
        return (
          <div>
            <Prompt
              when={!!this.state.name} <!-- Tell prompt should happen -->
              message={location => `Are you sure you want to go to ${location.pathname}`} <!-- if return string, then prompting, if return true, then allow-->
            />
            <div>
              <div>Nice looking profile! What's your name?</div>
              <input value={this.state.name} onChange={e => this.setState({ name: e.target.value })} />
            </div>
          </div>
        );
      }
    }
    
    export default Profile;

  • 相关阅读:
    Java 之Integer相等比较
    Java 之HashMap.values()方法误用
    Java 异常归纳总结
    MySQL之GROUP BY用法误解
    Java 类的成员初始化顺序
    十三、java_GUI
    十二、java_网络编程
    十一、java线程
    十、java_IO
    九、java容器
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8721046.html
Copyright © 2020-2023  润新知