无法读取undefined的属性'props' - React Router

时间:2017-01-07 14:26:48

标签: javascript reactjs react-router

我正在尝试使用react服务器进行嵌套路由。但我在浏览器控制台中收到错误

Uncaught TypeError: Cannot read property 'props' of undefined

这是反应代码

    const App = () => {
    return (
        <div>
            <h2>{'Nested Routing App'}</h2>
             <ul>
                <li>
                    <Link to="/view1">{'View1'}</Link>
                </li>
            </ul>
            {this.props.children}
        </div>
    );
};

const View1 = () => {
    return(
        <h3>{'Welcome to View1'}</h3>
    );
};


render(
    <Router>
        <Route component={App} path="/">
            <Route component={View1} path="/view1"></Route>
        </Route>
    </Router>,
document.getElementById('app'));

知道可能出现什么问题吗?

1 个答案:

答案 0 :(得分:5)

箭头功能没有词汇this。功能组件将props作为函数参数接收。所以正确的方法是......

const App = (props) => {
    return (
        <div>
            <h2>{'Nested Routing App'}</h2>
             <ul>
                <li>
                    <Link to="/view1">{'View1'}</Link>
                </li>
            </ul>
            {props.children}
        </div>
    );
};

在此处阅读更多内容https://facebook.github.io/react/docs/components-and-props.html