i make toogle in react using useState like this sample component
import React, { useState, useEffect } from "react";
const Button = () => {
const [show, setShow] = useState(false)
useEffect(() => {
function listing () {
setShow(false)
}
if (show && true) {
window.addEventListener("click", listing, true)
console.log("test")
}
})
return (
<>
<button onClick={() =>
setShow(true)
}>
test
</button>
{show && <div> show text</div>}
</>
)
}
export default Button
but if button already true value and im click again button value is true and im click window element be false, so element target div show cant gone, but is same if im give setShow(!show) on listener onclick button. target element keep show if already true
2
Answers
im find answer https://stackoverflow.com/a/45323523/16945930
and i add liltle code
I think this is something that you want to create: