反应这个道具得到空对象问题

时间:2017-03-18 08:44:12

标签: reactjs url-routing

在render方法中用内部组件反应js,我把控制台日志this.props获取一个空对象值!这是我的代码,

    import React, { Component } from 'react';
import { Router, Route, Link, IndexRoute, hashHistory, browserHistory } from 'react-router';

import Home from './home';
import About from './about';


class App extends React.Component {
    constructor(){
        super();
    }
  render() {
    console.log(this.props)
    return (
        <div>
            Hi
        </div>
    );
  }
}

export default App

1 个答案:

答案 0 :(得分:0)

您可以使用withRouter高阶组件:

import React, { Component } from 'react';
import { Router, Route, Link, IndexRoute, hashHistory, browserHistory, withRouter } from 'react-router';

import Home from './home';
import About from './about';


class App extends React.Component {
    constructor(){
        super();
    }
  render() {
    console.log(this.props)
    return (
        <div>
            Hi
        </div>
    );
  }
}

export default withRouter(App)

More info here.