Design comparison
Solution retrospective
This was a fairly easy project but I did have 2 small problems that I had to work around. My main question is about the navbar.
First, I had to set the variable with a useState because the querySelector was responding with a null - is this a good way to set variables? I also had to make another function to close the mobile nav when a link inside was clicked because it would redirect to the correct page but the mobile nav would stay open.
Thanks so much for all critiques + comments!
Community feedback
- @markup-mitchellPosted about 4 years ago
Hi Diarrah, it looks great!
You can make it even easier than that by using the state as the source of truth and letting the DOM update accordingly, rather than fetching values into state from the document.
Declare a single state variable with a default value:
let [menuOpen, setMenuOpen] = useState(false);
You can make all kinds of things conditional on that in the render method - whether the overlay appears, which image is used in the button, even the appearance of whole components:
{ menuOpen ? <MobileMenu /> : null; }
You can use a single function to toggle the state to its opposite with
<button onClick={ () => setMenuOpen(!menuOpen)}>
And you can make the mobile menu close automatically by calling
setMenuOpen(false)
in response to a change in routing (I don't know the react-router API well enough to suggest how that's done - I think it's differnt from the Next.js router I used here).Hope that helps?! I was trying to be as brief as possible!
2@DiarrahPosted about 4 years ago@markup-mitchell Yes! This would work so much better & I don't know why I didn't think of this earlier lol. I willi implement this after I get off work later.
Thank you!!
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord