Skip to content

Instantly share code, notes, and snippets.

@ThaddeusJiang
Created July 31, 2018 12:00
Show Gist options
  • Save ThaddeusJiang/ddaf87ff1144843d16e3d2204bbc88c6 to your computer and use it in GitHub Desktop.
Save ThaddeusJiang/ddaf87ff1144843d16e3d2204bbc88c6 to your computer and use it in GitHub Desktop.
React Route 路由变化时记录log

使用 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。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment