zoukankan      html  css  js  c++  java
  • [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;

  • 相关阅读:
    bzoj1648
    bzoj3404
    bzoj1650
    bzoj1625
    bzoj1606
    bzoj1464
    bzoj1572
    bzoj1617
    bzoj1092
    bzoj1091
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8721046.html
Copyright © 2011-2022 走看看