4

Please excuse me if I forget anything - this is my first post on Stack Overflow.

The function's purpose is to open and close my hamburger menu. I have attached a snapshot of the JSX file where this issue is: NavbarLinks.jsx. The lines of code that I am struggling with are 15 and 18 where I am trying to declare a CSS module class as a variable.

const [navLinkOpen, navLinkToggle] = useState(false);
  const handleNavLinksToggle = () => {
    navLinkToggle(!navLinkOpen);
  };

  const renderClasses = () => {
    let classes = {`${styles.navLinks}`};

    if (navLinkOpen) {
      classes += {`${styles.active}`};
    }

    return classes;
  };

I then call the returned class inside the ul tag on line 27.

<ul className={renderClasses}>

I have tried several variations of declaring this class as a variable, what I am showing you here is just my last attempt. I am aware that the attempt I am using has broken the code. - I am fairly new to JavaScript and instead of simply following YouTube guides, I learn better by trying things myself.

Please try and answer the question I have instead of suggesting an alternative method, purely because I just want to learn more about this! - but if you do have alternative methods of what I am trying to achieve here, I would very much like to see them too.

I'll also include a snapshot of the CSS Module file that I am working with just incase you may find it useful: NavbarLinksStyle.module.css

Thanks in advance for any help - will be around to answer any questions.

2 Answers 2

1

first of all, congrats on the post. It's very well explained. The code you shared seems to be pretty fine overall, but from what I can see you are missing the execution of the renderClasses function.

const [navLinkOpen, navLinkToggle] = useState(false);
  const handleNavLinksToggle = () => {
    navLinkToggle(!navLinkOpen);
  };

  const renderClasses = () => {
    let classes = styles.navLinks;

    if (navLinkOpen) {
      classes += styles.active;
    }

    return classes;
  };

  {...}
    <ul className={renderClasses()}> 
  {...}

Also, you can achieve the same result by using a variable instead of a function:

const renderClasses = `${styles.navLinks} ${navLinkOpen ? styles.active : ''}`;

EDIT: Also as @Mina pointed out you should get rid of the curly braces on your render function.

Sign up to request clarification or add additional context in comments.

4 Comments

Amazing, thank you! I have just taken a break to spend some time with my partner who has has to put up with me stressing over this on a weekend 😂 but I’ll let you know how it goes in 30 mins. Thanks for the amazing response!
It worked! So simple! - I guess this is all just part of the learning process. I was reluctant to make a Stack Overflow account, I didn't think anybody would bother answering, I think you've just created a monster by showing me how useful this website is! Thank you again
Okay, how about this one... (I think my brain has turned to mush because it seems like it should be so simple). How do I now change the hamburgerToggle class to color:white when the ul tag is active? The hamburger icon is outside of the ul tag. I'm going to spend hours on this again, aren't I?
Since we know ul will be active when navLinkOpen is true, you can just toggle the class depending on the state: <div className={navLinkOpen ? styles.buttonToggleActive : styles.buttonToggle} />
0

First of all, you shouldn't declare the classes variable with curly brackets.

  const renderClasses = () => {
    let classes = styles.navLinks

    if (navLinkOpen) {
      classes += styles.active
    }

  return classes;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.