2017-07-23 115 views
0

我有一個導航欄組件,我試圖放在一起,並且我的App.js中有一個問題,我調用了所有組件。爲了使它工作,我必須渲染兩次導航欄,但它顯示了兩次,其中只有第二個導航欄功能正常。在渲染中取出兩行代碼中的任意一行都會導致錯誤,其中只有導航欄的文本出現(但只顯示一次,而不是兩次),並且不可點擊。React路由器組件渲染兩次

這裏是渲染()的導航欄呈現兩次:

render() { 
    return (
     <div> 
     <Login /> 
     <Search /> 
     <BrowserRouter> 
      <div className='container'> 
       <Navbar /> 
       <Route component={Navbar}/> 
       <Route exact path="/" component={Home}/> 
       <Route path="/bios" component={Bios}/> 
       <Route path="/message" component={Message}/> 
      </div> 
     </BrowserRouter> 
     </div> 

    ); 
    } 

Only the lower text is functional, and taking out either of the Navbar lines in my render function above get rid of the first navbar, but it is not functional.

+1

這就是爲什麼 <路徑成分= {導航欄} /> – Sergey

+0

作爲布林說,我建議以除去'<路徑成分= {導航欄} />'如它不提供與''只是不必要的。 –

回答

0

我修復了這個問題。早些時候,我得到的錯誤是因爲下面的語句2:

<Navbar /> 
    <Route component={Navbar}/> 

其中一人被渲染到其他組件,顯然如果兩個組件重疊,甚至部分,它的功能消失。我甚至不能說它重疊,因爲組件周圍有透明的填充。我所做的只是刪除了第二條語句,並在我的CSS中設置了導航欄的樣式,以便它不再重疊。

0

您可以外接希望你<BrowserRouter>的作爲的NavBar我猜它總是呈現,而不是依賴於一些匹配路線,即

render() { 
    return (
     <div> 
     <Login /> 
     <Search /> 
     <Navbar /> 
     <BrowserRouter> 
      <div className='container'> 
       <Route exact path="/" component={Home}/> 
       <Route path="/bios" component={Bios}/> 
       <Route path="/message" component={Message}/> 
      </div> 
     </BrowserRouter> 
     </div> 
    ); 
    } 
+0

我試圖將其更改爲您提供的呈現代碼,但我得到'TypeError:無法讀取未定義'錯誤的屬性'路徑'。查找這個錯誤建議用包裝所有的,這已經完成了。任何想法這個錯誤是什麼意思? – Anon

+0

該錯誤在哪裏發生? – Tony