[英]How to handle events with onClick in ReactJS
I am fairly new to ReactJs and trying to have a static sidebar menu on the left with Overview, Performance and Stability as the choices.我对 ReactJs 相当陌生,并试图在左侧有一个 static 侧边栏菜单,其中有概述、性能和稳定性作为选项。 Then I want to have a section to the right of the menu that displays the results from the selection of the 3 choices on the left menu.
然后我想在菜单右侧有一个部分,显示左侧菜单中 3 个选项的选择结果。 I have the below code so far and using grid to separate the two sections.
到目前为止,我有以下代码并使用网格来分隔两个部分。 I am trying to accomplish 3 things when a user clicks one of the menu options.
当用户单击菜单选项之一时,我正在尝试完成 3 件事。 1st when they click on Overview for example then the right grid section needs to load the js that is imported from overview folder.
例如,当他们单击概览时,第一次右侧网格部分需要加载从概览文件夹导入的 js。 2nd I need to also highlight the Overview menu selection in order to be able to let the user know what they have selected.
第二,我还需要突出显示概览菜单选择,以便能够让用户知道他们选择了什么。 3rd I need for the page to always land on Overview selection by default when the page is opened.
第三,当页面打开时,我需要默认页面始终位于概览选项中。 Please let me know what I need to add to the code below in order to accomplish the 3 things.
请让我知道我需要在下面的代码中添加什么才能完成这 3 件事。 Thanks.
谢谢。
import Overview from "./overview";
import Performance from "./performance";
import Stability from "./stability";
export default class Menu extends React.Component {
render() {
return (
<Grid>
<GridItem columnSpan = {3}>
<h3> Menu selection </h3> <br/>
<h1> Overview </h1> <br/>
<h1> Performance </h1> <br/>
<h1> Stability </h1>
</GridItem>
<GridItem columnSpan = {9}>
<h3> Info </h3> <br/>
</GridItem>
</Grid>
);
}
}
I created a sample code with the minimum change i can do to your code.我创建了一个示例代码,我可以对您的代码做最少的更改。 But i must remind you that this is not (not even near) the best way to handle it.
但我必须提醒你,这不是(甚至接近)处理它的最佳方法。
import Overview from "./overview";
import Performance from "./performance";
import Stability from "./stability";
export default class Menu extends React.Component {
constructor(props){
super(props);
this.state = {
selectedView: "overview"
}
}
changeView = (view) => {
this.setState({selectedView: view});
}
render() {
return (
<Grid>
<GridItem columnSpan = {3}>
<h3>Menu selection</h3> <br/>
<h1 onClick={() => this.changeView("overview")}> Overview </h1> <br/>
<h1 onClick={() => this.changeView("performance")}> Performance </h1> <br/>
<h1 onClick={() => this.changeView("stability")}> Stability </h1>
</GridItem>
<GridItem columnSpan = {9}>
<h3>Info</h3> <br/>
{this.state.selectedView === 'overview' && <Overview />}
{this.state.selectedView === 'performance' && <Performance />}
{this.state.selectedView === 'stability' && <Stability />}
</GridItem>
</Grid>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.