【发布时间】:2019-11-21 07:53:07
【问题描述】:
所有关于如何制作 reactJS 网站的新示例都显示了类似的示例
const NavBar = (params) => {
return(
<div>
text
</div>
)
}
export default NavBar;
然而,material-UI 似乎显示了以下内容,这让我很难理解如何让选项卡与上面的新代码格式一起工作。构造函数在新格式下如何工作?
import React from 'react';
import {Tabs, Tab} from 'material-ui/Tabs';
const styles = {
headline: {
fontSize: 24,
paddingTop: 16,
marginBottom: 12,
fontWeight: 400,
},
};
export default class TabsExampleControlled extends React.Component {
constructor(props) {
super(props);
this.state = {
value: 'a',
};
}
handleChange = (value) => {
this.setState({
value: value,
});
};
render() {
return (
<Tabs
value={this.state.value}
onChange={this.handleChange}
>
<Tab label="Tab A" value="a">
<div>
<h2 style={styles.headline}>Controllable Tab A</h2>
<p>
Tabs are also controllable if you want to programmatically pass them their values.
This allows for more functionality in Tabs such as not
having any Tab selected or assigning them different values.
</p>
</div>
</Tab>
<Tab label="Tab B" value="b">
<div>
<h2 style={styles.headline}>Controllable Tab B</h2>
<p>
This is another example of a controllable tab. Remember, if you
use controllable Tabs, you need to give all of your tabs values or else
you wont be able to select them.
</p>
</div>
</Tab>
</Tabs>
);
}
}
编辑
我找到了我需要做的事情 通过使用 useState,我可以检查用户在哪个页面上,并确保这是用户第一次进入该站点,然后设置一个 const,然后允许我运行另一个 const 来设置需要处于活动状态的选项卡。
const firstTime = false;
React.useState(() => {
if(!firstTime && window.location.pathname === "/two"){
//console.log('mounted or updated')
firstTime = 1;
}
}
);
const [value, setValue] = React.useState(firstTime);
【问题讨论】:
-
什么是“新格式”?你是说上课吗?这不是新的。你的问题不清楚
-
您的问题是关于功能组件的吗?如何在功能组件中使用状态?
标签: reactjs material-ui