繁体   English   中英

react-router如何访问this.props

[英]react-router how to access this.props

如何从<Router>onUpdate访问this.props 我需要能够看到当前路线是什么,我需要访问this.props.route我想确定它是哪个页面( name )。 在此处输入图片说明 我找不到一些资源。 我有这样的代码(现在它检查window.location以确定它是什么页面,但是问题很严重,因为它可以是任何其他静态页面):

const logPageViews = () => {
  if (_.startsWith(window.location.pathname, '/search/')) {
    let query = window.location.pathname.replace('/search/', '').replace(/--/g, '/').replace(/-/g, '+');
    if (!_.isEmpty(window.location.search)) {
      query += `&${window.location.search.replace('?', '')}`;
    }

    ga.pageview(`/search?query=${query}`);
  } else {
    ga.pageview(window.location.href.replace(window.location.origin, ''));
  }

  fbq.pageView();
}

const reducer = combineReducers(reducers);
const store = applyMiddleware(...middlewares)(createStore)(reducer, initialState);

ReactDOM.render(
  <Provider store={store}>
    <Router routes={getRoutes(store)} history={browserHistory} onUpdate={() => {logPageViews()}} />
  </Provider>,
  document.getElementById('app')
);

我有两个可能的答案。 首先,您是否可以像下面的演示一样从州访问当前路线? https://github.com/reactjs/react-router/issues/1539

其次,当您调用getRoutes(store)时,是否可以访问路由数组的最后一个元素?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM