Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Created July 5, 2023 20:03
Show Gist options
  • Save ernestlv/a5662c2d5540d85464693fae0e639e9c to your computer and use it in GitHub Desktop.
Save ernestlv/a5662c2d5540d85464693fae0e639e9c to your computer and use it in GitHub Desktop.
#React conditional render
const Square = () => { // Root Functional Component
let [show, setShowMe] = React.useState(false); // React Hook
const click = () => { // 'this' points to window
setShowMe(!show);
}
console.log("render square");
// View
return (
<div style={{color:'white'}}>
<div style={{backgroundColor:'red'}}>Hello</div>
{
show && (<div style={{backgroundColor:'blue'}}>World!!!</div>)
}
<button onClick={click}>show/hide</button>
</div>
);
}
const root = ReactDOM.createRoot(document.querySelector("#app"));
root.render(<Square />);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment