Skip to content

Instantly share code, notes, and snippets.

@rileybathurst
Last active February 18, 2023 18:09
Show Gist options
  • Save rileybathurst/a3fa139cbd4301c3a7d3cb319a0bdd42 to your computer and use it in GitHub Desktop.
Save rileybathurst/a3fa139cbd4301c3a7d3cb319a0bdd42 to your computer and use it in GitHub Desktop.
Using setState on a React menu
import React, { useState } from 'react';
function MenuButton() {
let [menu, setMenu] = useState('closed');
if (menu === 'open') {
return (
<menu__small
style={{
height: '2rem',
}}
>
<button onClick={() => setMenu(menu = 'closed')}>Close</button>
<ul>
<li>{menu}</li>
<li>hey</li>
<li>1223</li>
</ul>
</>
);
} else {
return (
<>
<button onClick={() => setMenu(menu = 'open')}>Open</button>
<p>{menu}</p>
</>
);
}
}
// markup
const MenuPage = () => {
return (
<main>
<h1>Menu</h1>
<MenuButton />
<p><span role="img" aria-label="unicorn">🦄</span></p>
</main>
)
}
export default MenuPage
@rileybathurst
Copy link
Author

I'm still not sure this is right it does re-renders but isn't that the point of react

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment