我有傳遞id到我的產品頁面的問題,我嘗試eveything和搜索答案,但它仍然不工作。反應this.props.params undefined
這裏是我的index.js:
import React from "react";
import {render} from "react-dom";
import {Router, Route, IndexRoute, hashHistory} from "react-router";
import {Menu} from './components/Menu';
import {MainPage} from './components/MainPage';
import {DetailsProduct} from './components/DetailsProduct';
class App extends React.Component{
render(){
return(
<Router history={hashHistory}>
{/* <IndexRoute component={Menu}></IndexRoute> */}
<Route path="/" component={()=>(<div><Menu/><MainPage/></div>)}></Route>
<Route path={"product/:id"} component={()=>(<div><Menu/><DetailsProduct>asd</DetailsProduct></div>)}></Route>
</Router>
)
}
}
render(<App/>, window.document.getElementById("app"));
而且DetalisProduct (頁:http://localhost:8080/#/product/1)
import React from "react";
export class DetailsProduct extends React.Component{
render(){
console.log(this.props.params); <-- this is undefined
return(
<h1>Product</h1>
)
}
}
你希望在'this.props.params'中看到什麼? – Andrew
我沒有看到你正在傳遞道具給? –
它現在的工作,但我必須通過兩個組件「菜單和DetailsProduct」,你知道我怎麼能做到這一點? –