使用 JSX 的 onEnter。 onEnter 是 React Route 提供的一个钩子,在进入页面时被调用。 与之对应的是 onLeave。
例子🌰:
function temp() {
console.log('Hi');
}
const routes = (
<Switch>
<Route path='/path1' component={comp1} onEnter={temp} />
<Route path='/path2' component={comp2} onEnter={temp} />
</Switch>
)
你也可以在 temp() 中发 Ajax 向 Server 写log。