我有一個非常標準的設置,配頁陣營this.props未定義
import React from "react";
import ReactDOM from "react-dom";
import { IndexRoute, Router, Route, Link, hashHistory as history } from "react-router";
import Layout from "./pages/Layout";
...
import User from "./pages/User";
ReactDOM.render(
<Router history={history}>
<Route path="/" component={Layout}>
<IndexRoute component={...}/>
<Route path="project/create" component={...}/>
<Route path="project/:id" component={...}/>
<Route path="user/:id" component={User}/>
<Route path="*" component={...}/>
</Route>
</Router>,
document.getElementById("app-root"));
一切工作完全不同的,當我去一個網頁一樣site.tld/#/user/5
的User
成分有一定的麻煩實例化一個路由器正常。所有其他網頁正在工作,我也有另一個頁面使用網址參數(project/:id
),它也工作得很好。
import React from "react";
...
export default class User extends React.Component {
constructor() {
super();
console.log(this);
...
}
render() {
return ...
}
這是我在控制檯中看到
我敢肯定它是最愚蠢的事情以後再,但我不能把它挑出來......
通常,您將構造函數中收到的道具傳遞給組件超類:https://discuss.reactjs.org/t/should-we-include-the-props-parameter-to-class-constructors- when-declaring-components-using-es6-classes/2781 –
嘗試在'render()'方法中記錄'this.props'來驗證。 –
@HunterMcMillen你是對的,道具在渲染中可用,但不在構造函數中,在構造函數中傳遞道具使得我的代碼沒有區別:/ –