核心提示:跳转前确认React Router 提供一个routerWillLeave生命周期钩子,这使得 React组件可以拦截正在发生的跳转,或在离开route前提示用户。routerWillLeave返回值...
跳转前确认
React Router 提供一个routerWillLeave生命周期钩子,这使得 React组件可以拦截正在发生的跳转,或在离开route前提示用户。routerWillLeave返回值有以下两种:
returnfalse取消此次跳转
return返回提示信息,在离开 route 前提示用户进行确认。
你可以在route 组件中引入Lifecyclemixin 来安装这个钩子。
import { Lifecycle } from 'react-router' const Home = React.createClass({ // 假设 Home 是一个 route 组件,它可能会使用 // Lifecycle mixin 去获得一个 routerWillLeave 方法。 mixins: [ Lifecycle ], routerWillLeave(nextLocation) { if (!this.state.isSaved) return 'Your work is not saved! Are you sure you want to leave?' }, // ... })
如果你在组件中使用了 ES6 类,你可以借助react-mixin包将Lifecyclemixin 添加到组件中,不过我们推荐使用React.createClass来创建组件,初始化路由的生命周期钩子函数。
如果你想在一个深层嵌套的组件中使用routerWillLeave钩子,只需在route 组件中引入RouteContextmixin,这样就会把route放到 context 中。
import { Lifecycle, RouteContext } from 'react-router' const Home = React.createClass({ // route 会被放到 Home 和它子组件及孙子组件的 context 中, // 这样在层级树中 Home 及其所有子组件都可以拿到 route。 mixins: [ RouteContext ], render() { return} }) const NestedForm = React.createClass({ // 后代组件使用 Lifecycle mixin 获得 // 一个 routerWillLeave 的方法。 mixins: [ Lifecycle ], routerWillLeave(nextLocation) { if (!this.state.isSaved) return 'Your work is not saved! Are you sure you want to leave?' }, // ... })