iJa*_*ade 0 javascript reactjs react-router
我正在尝试使用react服务器进行嵌套路由.但我在浏览器控制台中收到错误说明
Uncaught TypeError: Cannot read property 'props' of undefined
Run Code Online (Sandbox Code Playgroud)
这是反应代码
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'));
Run Code Online (Sandbox Code Playgroud)
知道什么可能是错的吗?
箭头函数没有词法this.功能组件将props作为函数参数接收.所以正确的方法是......
const App = (props) => {
return (
<div>
<h2>{'Nested Routing App'}</h2>
<ul>
<li>
<Link to="/view1">{'View1'}</Link>
</li>
</ul>
{props.children}
</div>
);
};
Run Code Online (Sandbox Code Playgroud)
在此处阅读更多内容https://facebook.github.io/react/docs/components-and-props.html
| 归档时间: |
|
| 查看次数: |
2558 次 |
| 最近记录: |