Every time you switch a route, a prompt pops up asking if you are sure and how to trigger it globally.

question:

this is a code from Ruan Yifeng"s react-router:
address: http://www.ruanyifeng.com/blo. slide to the bottom

how can I put it into the overall situation through this code? Which file should I put in?

my index.js:

import $ from "jquery"
import React from "react"
import ReactDOM from "react-dom"
import AppRouter from "./router"
import { Provider } from "react-redux"
import LocaleProvider from "antd/lib/locale-provider"
import configureStore from "./store"
import "./assets/style/index.less"
import zhCN from "antd/lib/locale-provider/zh_CN";

const store = configureStore();


class Root extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <LocaleProvider locale={zhCN}>
        <Provider store={store}>
          <div>
            <AppRouter store={store} />
          </div>
        </Provider>
      </LocaleProvider>
    )
  }
}
ReactDOM.render(<Root />, document.getElementById("p_app"))


can I put it here?

Jun.07,2021

of course you can put it here.


use high-level components:

import Immutable from "immutable"
import React from "react"
import { withRouter } from "react-router"

const routerLeaveHookHoc = WrappedComponent => {
    //withRouterrouterLeaveHookHoc 
    return withRouter(
        class extends React.Component {
            componentDidMount() {
                console.log("didmount");
                this.props.router.setRouteLeaveHook(
                    this.props.route,
                    this.routeLeaveHook
                );
            }

            routeLeaveHook(a){
              //  false 
              // 
            }
            render() {
                return <WrappedComponent {...this.props}/>
            }
        }
    )
}
Menu