【发布时间】:2020-06-07 10:13:36
【问题描述】:
我对 React 还很陌生,目前在使用 react 钩子时遇到了一些问题。我在构造函数中初始化了 show 和 setShow ,但是我得到一个错误,说 show 和 setShow 在渲染函数中是未定义的。我有类似的东西:
export default class Projects extends Component {
constructor(props) {
super(props);
const [show, setShow] = useState(false); //Where I initialize show and setShow
const handleClose = () => setShow(false);
const handleShow = () => setShow(true);
}
render() {
return (
<div class="projects-section" id="projects">
<img className="project-image" src="assets/UAS-frontend.png" onClick={handleShow}/>
<Modal show={show} onHide={handleClose}> //Where I get the error
<Modal.Header closeButton></Modal.Header>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
Close
</Button>
<Button variant="primary" onClick={handleClose}>
Save Changes
</Button>
</Modal.Footer>
</Modal>
</div>
)
}
}
【问题讨论】:
-
You can’t use Hooks inside of a class component, but you can definitely mix classes and function components with Hooks in a single tree。看到这个话题stackoverflow.com/questions/53371356/…
标签: reactjs react-hooks