react router this.props.children渲染问题
使用react-router-dom的route标签的render方法,组件里使用this.props.children渲染不出来
相关环境 react16
相关代码
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
app.js
import React, { Component } from 'react';
import MyLayout from './components/Layout/Layout';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import 'antd/dist/antd.css';
import Login from './page/Login/Login';
import Home from './page/Home/Home';
import ArticleList from './page/ArticleList/ArticleList';
import NewArticle from './page/NewArticle/NewArticle';
class App extends Component {
render() {
let LayoutRouter = (
<MyLayout>
<Switch>
<Route exact path="/" Component={Home} />
<Route path="/article/list" Component={ArticleList} />
<Route path="/article/new" Component={NewArticle} />
</Switch>
</MyLayout>
);
return (
<Router>
<Switch>
<Route path="/login" Component={Login} />
<Route path="/" render={ props => LayoutRouter } />
</Switch>
</Router>
)
}
};
export default App;
MyLayout组件
import React, { Component } from 'react';
import { Layout } from 'antd';
import SideNav from '../SideNav/SideNav';
import TopNav from '../TopNav/TopNav';
const { Content } = Layout;
class MyLayout extends Component {
render() {
console.log(this.props.children);
return (
<Layout className="myLayout">
<SideNav />
<Layout>
<TopNav />
<Content style={{margin: '0 16px'}} >
内容
{this.props.children}
</Content>
</Layout>
</Layout>
)
}
}
export default MyLayout;
Home.js
import React, { Component } from 'react';
class Home extends Component {
render() {
return <div>首页</div>
}
}
export default Home;
运行之后的结果
其中"内容"两个字渲染出来了,但是this.props.children没有渲染出来,也没有报错
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你Route上的
component
应该小写吧?嵌套路由也用component,代码如下: