I want to show a text on the screen and only hide it when a button is pressed, but I don't know how to do it. I want to use useState to achieve this effect:
const [textVisibility, setTextVisibility] = useState(true)
The problem I found is that when the button is clicked, the page will re-render and the visibility value will change to the default value (true). what should I do?
Idk what are you experiencing but for me it works fine the following code:
import React from 'react'; import {useState} from 'react'; export function App(props) { const [textVisibility, setTextVisibility] = useState(true) return ( {textVisibility && ); }setTextVisibility(!textVisibility)}>Hello React.
}const App(){ const [isVisible, setIsVisible] = useState(false) return ( <> {isVisible ? : null } > ) }