码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-05 11:40:23      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:message   fun   mes   flow   eth   imp   border   reac   radius   

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;

 

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

标签:message   fun   mes   flow   eth   imp   border   reac   radius   

原文地址:https://www.cnblogs.com/Answer1215/p/8721046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!