Javascript 反应课如何主播波波
嗨,我是一个新的反应和钩像useState。我很难理解这个概念以及如何使用它。 所以我不想制作比我现在更复杂的片段和文件 我在将React.component从函数切换到类时遇到问题 我已经做了一个尝试,以显示我想切换。 但是,我只是不能很好地理解文档,无法实现它 要使React.Class.Component使用React状态,需要做什么Javascript 反应课如何主播波波,javascript,reactjs,react-hooks,popover,use-state,Javascript,Reactjs,React Hooks,Popover,Use State,嗨,我是一个新的反应和钩像useState。我很难理解这个概念以及如何使用它。 所以我不想制作比我现在更复杂的片段和文件 我在将React.component从函数切换到类时遇到问题 我已经做了一个尝试,以显示我想切换。 但是,我只是不能很好地理解文档,无法实现它 要使React.Class.Component使用React状态,需要做什么 import React, { Component } from "react"; import { makeStyles } from "@mat
import React, { Component } from "react";
import { makeStyles } from "@material-ui/core/styles";
import Popover from "@material-ui/core/Popover";
import Typography from "@material-ui/core/Typography";
import Button from "@material-ui/core/Button";
// const useStyles = makeStyles(theme => ({
// typography: {
// padding: theme.spacing(2)
// }
// }));
export default class SimplePopover extends Component {
constructor(props) {
super(props);
// const classes = useStyles();
// const [anchorEl, setAnchorEl] = React.useState(null);
this.state = {
anchorEl: null,
open: false,
id: undefined
}
this.handleClick = this.handleClick.bind(this);
this.handleClose = this.handleClose.bind(this);
// const open = Boolean(anchorEl);
// const id = open ? "simple-popover" : undefined;
}
handleClick(event) {
this.setState({anchorEl: event.currentTarget, open: Boolean(event.currentTarget), id: "simple-popover"});
}
handleClose(event) {
this.setState({anchorEl: event.currentTarget, open: false, id: undefined});
}
render() {
return (
<div>
<Button
aria-describedby={this.id}
variant="contained"
color="primary"
onClick={this.handleClick}
>
Open Popover
</Button>
<Popover
id={this.id}
open={this.state.open}
anchorEl={this.state.anchorEl}
onClose={this.handleClose}
anchorOrigin={{
vertical: "bottom",
horizontal: "center"
}}
transformOrigin={{
vertical: "top",
horizontal: "center"
}}
>
{/* <Typography className={this.classes.typography}> */}
<div>The content of the Popover.</div>
{/* </Typography> */}
</Popover>
</div>
);
}
}
您不能在类组件中使用钩子,因此我必须使用React.Component函数才能使用React钩子?在React.Class.Function中没有“使用React钩子”?不能在类组件中使用“React钩子”,只能在功能组件中使用。类组件是那些包含类的组件,而函数是只包含函数的组件Hanks L.O.这看起来很痛苦-为单个类组件创建一个函数组件,但没关系。
const [anchorEl, setAnchorEl] = React.useState(null);