2017-05-05 15 views
2

パラメタとのルートをいくつか持っています(。/ user /:user、car /:car /:年) location.pathnameを手動で解析することを避けようとしています。反応ルータ(v3)を使用することが可能です。反応ルータv3で経路を一致させる

現在のURLの場所に一致するルートを見つける方法を教えてください。私は必要なものをあるかもしれないhttps://github.com/ReactTraining/react-router/blob/v3/modules/matchRoutes.js

if (router.match_to_route('/user/:user')) { 
    ... do something 
} 
... 

方法matchRoutes:のようなものが必要 。おかげさまで

更新日:

を私が反応し-ルータ-DOMを使ってこれを行っている

import { match } from 'react-router'; 

const routes = router.routes; 
match({ routes, location }, (error, redirect, renderProps) => { 
    const listOfMatchingRoutes = renderProps.routes; 
    if (listOfMatchingRoutes.some(route => route.path === '/user/:user')) { 
     ... 
    } 
} 

https://github.com/ReactTraining/react-router/issues/3312#issuecomment-299450079

答えて

1

によって達成することができます。コードを簡単に理解できるようにコードを簡略化しました。私はダッシュボードルートのあるparamユーザーをメインのAppコンポーネントに渡し、this.props.match.params.userという名前の子コンポーネントをDashboardという名前でアクセスしました。

App.jsは、私はそれはあなたを助けることを願っています

class App extends React.Component { 
constructor(props) { 
    super(props); 
    this.state = {open: false}; 
    this.state = {message: "StackOverflow"}; 
    } 

render(){ 
    return (
     <Router> 
      <div> 

      <Route exact path="/dashboard/:user" render={props => <Dashboard {...props} />} /> 
      <Route exact path="/information" render={props => <Information {...props} />} /> 
      </div> 
     </Router> 
    ); 
} 
} 

Dashboard.js

import React from 'react'; 


class Dashboard extends React.Component { 

    constructor(props) { 
    super(props); 
    } 

    render() { 

    return (
      <div ><h1> Hello {this.props.match.params.user}</h1></div> 
    ); 
    } 
} 

export default Dashboard; 

を提出します。

+0

親愛なるRohit、あなたの答えは正しいです。残念ながら、私が解決しようとしている問題には適用されません。ありがとう – DraganS

+0

Ok @DraganS、問題ありません –

関連する問題