I'm fairly new to React, and I am currently having some trouble using the react hooks. I initialize show and setShow in the constructor, yet I get an error saying that show and setShow are undefined in the render function. I have something like:
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. See this topic stackoverflow.com/questions/53371356/…