I\'d like to know how to toggle a boolean state of a react component. For instance:
I have boolean state check in the constructor of my component:
const
You could also use React's useState hook to declare local state for a function component. The initial state of the variable toggled has been passed as an argument to the method .useState.
import { render } from 'react-dom';
import React from "react";
type Props = {
text: string,
onClick(event: React.MouseEvent): void,
};
export function HelloWorldButton(props: Props) {
const [toggled, setToggled] = React.useState(false); // returns a stateful value, and a function to update it
return ;
}
render( console.log('clicked!')} />, document.getElementById('root'));
https://stackblitz.com/edit/react-ts-qga3vc