【发布时间】:2018-06-18 20:44:14
【问题描述】:
这是我的示例代码。
import React, { Component } from "react";
import Navpills from "./Navpills";
import Home from "./pages/Home";
import About from "./pages/About";
import Blog from "./pages/Blog";
import Contact from "./pages/Contact";
class PortfolioContainer extends Component {
state = {
currentPage: "Home"
};
handlePageChange = page => {
this.setState({ currentPage: page });
};
render() {
return (
<div>
<Navpills
currentPage={this.state.currentPage}
handlePageChange={this.handlePageChange}
/>
Based on `this.state.currentPage`, render the appropriate component
here.
</div>
);
}
}
export default PortfolioContainer;
说明如下:
现在将代码添加到PortfolioContainer,以便根据当前选择的页面,在Navpills 组件下方呈现不同的组件。示例:
在
this.state.currentPage === "About"时渲染About组件在
this.state.currentPage === "Blog"时渲染Blog组件在
this.state.currentPage === "Contact"时渲染Contact组件在
this.state.currentPage === "Home"时渲染Home组件
【问题讨论】:
-
这个reactjs.org/docs/conditional-rendering.html的文档中有一个页面
-
另外,欢迎来到stackoverflow。您并没有真正提出问题,而是粘贴了诸如家庭作业之类的内容?查看此页面了解如何提出一个好问题stackoverflow.com/help/how-to-ask
标签: javascript node.js reactjs if-statement jsx