2016-08-22 52 views
0

我下列如下所示的應用程序文件和主要成分的react redux tutorial但我得到的錯誤「提供給IndexRoute無效丙component。在IndexRoute」和Uncaught TypeError: Cannot read property 'props' of undefined陣營路由器不接受成分作爲丙

//app file 


import React from 'react'; 

import { render } from 'react-dom'; 


// Import Components 
import Main from './components/Main'; 
import Single from './components/Single'; 
import PhotoGrid from './components/PhotoGrid'; 

// import react router deps 
import { Router, Route, IndexRoute, browserHistory } from 'react-router'; 

const router = (
    <Router history={browserHistory}> 
    <Route path="/" component={Main}> 
     <IndexRoute component={PhotoGrid}></IndexRoute> 
     <Route path="/view/:postId" component={Single}></Route> 
    </Route> 
    </Router> 
) 

render(router, document.getElementById('root')); 

,我讀了它可能是做的cloneComponent並試圖與{React.cloneElement(this.props.children, {...this.props})}但無濟於事,以取代部分。

//main 

import React from 'react'; 
import { Link } from 'react-router'; 

const Main = React.createClass({ 
    render() { 
    return (
     <div> 
     <h1> 
      <Link to="/">Reduxstagram</Link> 
     </h1> 
     {React.cloneElement(this.props.children, this.props)} 
     </div> 
    ) 
    } 
}); 

export default Main; 
+0

嘗試使用'{this.props.children}'而不是'{React.cloneElement(this.props.children,this.props)}' – Alejandro

回答

0

您顯示的代碼沒有任何問題。根據錯誤消息,您的組件出現問題。因爲您沒有正確渲染組件,所以react-router不是作爲React組件。檢查您的文件。爲了確保,只需複製並粘貼源代碼。