2017-03-15 24 views
3

剛剛升級到react-router-dom 4.0.0。我所有的組件都是常規的class es或胖箭頭。他們全部使用export default ThatComponent出口。但我得到這個:在React Router v4中獲取未定義組件

未捕獲的錯誤:元素類型無效:期望一個字符串(對於內置組件)或一個類/函數(對於複合組件)但得到:未定義。您可能忘記將組件從其定義的文件中導出。請檢查渲染方法Router

// minimal showcase 
import { BrowserRouter, Match, Miss } from 'react-router'; 

const Router =() => (
    <BrowserRouter> 
    <div> 
     {/* both Match and Miss components below cause an error */} 
     <Match exactly pattern="/login" component={Login} /> 
     <Match exactly pattern="/frontpage" component={Frontpage} /> 
     <Match exactly pattern="/logout" render={() => (<div>logout</div>)} /> 
     <Miss component={NoMatch} /> 
    </div> 
    </BrowserRouter> 
); 

爲什麼在<Match>組件認爲其他組件是不確定的?

+0

你能分享你找到Match組件的位置嗎? –

+0

從這裏得到它:http://frontend.turing.io/lessons/react-router-4.html –

回答

5

react-router的最終版本中沒有Match也沒有Miss。你只需要使用Route。另外,您需要安裝並使用進口react-router-dom包使用BrowserRouter(請參閱React Router documentation)。

要以最小的變化你的榜樣的工作,你需要做到以下幾點:

請看看NoMatchExample in React Router docs,看看如何以及何時使用Switch

相關問題