2017-08-28 13 views
2

私はreact-router-domで作業しようとしていますが、内部の他のすべてのコンポーネントを開く「メイン」コンテナを定義するにはどうすればわかりませんか? は、古いバージョンでは、私はreact-router-domのルートが親コンテナで開く

<Router history={history}> 
    <Route path="/" component={Main}> 
     <IndexRoute component={ShowsPage} /> 

     <Route path="/shows" component={ShowsPage} /> 
     <Route path="/tickets" component={ticketsPage} /> 
    </Route> 

    <Redirect from="*" to="/" /> 
    </Router> 

を行うために使用し、それが魔法のように働くだろう、今私はNOTFOUNDルートを実装するかどうかはわかりません。この

<Router> 
    <div> 
     <Route component={Main}/> 
     <Route path="/" exact component={ShowsPage}/> 
     <Route path="/tickets" exact component={ticketsPage}/> 
     <Route path="/shows" exact component={ShowsPage}/> 
    </div> 
</Router> 

のようなものを試してみましたが、中私の最後の試みは私がコンテナの中でそれらを開けるように管理しましたが、私はこの警告も受け取りますThe prop `children` is marked as required in `Main`, but its value is `undefinedと私はなぜそれらを得るのだろうか?私は何が間違っていますか?そしてどのように私は他のすべてのルートが(いただきましそばに定義された)に「/」

ここ

はMain.js(私のメインコンテナ)

import React from 'react'; 
import PropTypes from 'prop-types'; 

import HeaderContainer from './HeaderContainer'; 
import FooterContainer from './FooterContainer'; 

const Main = ({children}) => (
    <div> 
     <container className="containerHeader"> 
      <HeaderContainer /> 
     </container> 
     <container className="containerMain"> 
      {children} 
     </container> 
     <container className="containerFooter"> 
      <FooterContainer /> 
     </container> 
    </div> 
) 

Main.propTypes = { 
    children: PropTypes.object.isRequired, 
} 


export default Main; 

おかげで(ShowsPageコンポーネントに)リダイレクトされることを設定することができます!

答えて

1

のようなスイッチ(反応-ルータ-DOMからのインポート)内部Main.Placeそれらのrenderメソッドでルートを追加する必要がありますShowsPageticketsPageShowsPageMainの中に表示します。 あなたがMainコンポーネントの内部でこれらのコンポーネントルートを配置する必要があり

Router

import createBrowserHistory from 'history/createBrowserHistory' 
const browserHistory = createBrowserHistory(); 

<Router history={browserHistory}> 
    <div> 
     <Route path="/" component={Main} /> 
    </div> 
</Router> 

history小道具を追加するメイン(親)コンポーネントの内側の子コンポーネントのルートを移動します。

const Main = ({ match }) => { //assume main component. 
    return (<div> 
     This is the Root component. 
     <Route path={`${match.url}`} exact component={ShowsPage} ></Route> 
     <Route path={`${match.url}tickets`} exact component={ticketsPage}></Route> 
     <Route path={`${match.url}shows`} exact component={ShowsPage}></Route> 
    </div>) 
}; 
+0

恐ろしいです!それは動作しますが、私はルートをプログラムで今変更することができます( 'ticketsPage'から' ShowsPage'へのルートを切り替えたいとしますか?) ありがとうございます。 – greW

+0

はリダイレクトを意味しますか? –

2
<Route component={Main}/> 
<Route path="/" exact component={ShowsPage}/> 
<Route path="/tickets" exact component={ticketsPage}/> 
<Route path="/shows" exact component={ShowsPage}/> 

<Route component={Main} /> 

あなたと仮定すると、この

<Switch> 
<Route path="/" exact component={ShowsPage}/> 
<Route path="/tickets" exact component={ticketsPage}/> 
<Route path="/shows" exact component={ShowsPage}/> 
</Switch> 
+0

警告:2つの警告が表示されます。警告:を同じルートに使用しないでください。 <ルートの子>は無視されます '' 'と' ''警告:失敗した小道具の種類:小道具の子は 'Main'で必須とマークされていますが、その値は' '未定義です.''' – greW

+0

答えて、それが動作するかどうか教えてください –

+0

それはそれを意味しましたか? https://pastebin.com/UNNmqHdUもしそうなら '' '警告:失敗した小道具のタイプ: '小児'の小道具は 'Main'で必須とマークされていますが、その値は 'undefined'''''です。定義されていないものにURLを変更しようとすると、内部にShowsPageコンポーネントがなくてもMainコンポーネントが得られます。 (私が最初に見せたように、私が達成したように)すべてがうまく機能します。おかげで@RaviTeja Teja – greW

関連する問題