0

How can I implement CSS change(colors... font-size... etc) with click without toggling?

menu_button.addEventListener('click', ()=>{
    bar.style.visibility = 'visible';
})

like this javascript, I just implement it just one side but I could not find how to do like this.

All reacts examples with css changes that I show implements with state...

visibility: ${props => props.isHidden ? 'visible' : 'hidden'};
<NavList isHidden={navClicked}></NavList>
<SubNavButton
  onClick={()=>{
    console.log(navClicked);
    setnavClicked(!navClicked);
  }}
> 
</SubNavButton>

just like this. I just want to do it without toggle.

3
  • Ah, I may've misunderstood your question. Are you trying to toggle something in NavList based on something in SubNavButton being clicked? Or are you trying to toggle some style within the same styled-component without going through a state update? Commented Jan 28, 2021 at 6:45
  • @DrewReese First one... Not same component Commented Jan 28, 2021 at 6:48
  • I think @djolf has it then. Sorry for my confusion. There's no way around it, you'll need to toggle some state to pass the updated prop to the component you want to toggle style of. Commented Jan 28, 2021 at 6:50

2 Answers 2

1

It's actually very easy. Using your example:

<NavList isHidden={navClicked}></NavList>
  <SubNavButton onClick={()=>{
    console.log(navClicked);
    setnavClicked(false); //set it one way, so state won't change when you click again.
  }}> 
</SubNavButton>
Sign up to request clarification or add additional context in comments.

Comments

0

You will need to use a state variable for this, as the state of the component will change with the click of the button.

If you want to set the visibility of NavList in the onClick event handler of SubNavButton, then you will have to use a state variable for the style property of the NavList. so the initial value of state variable 'visibility' can be 'visible' and onClick it can be set to 'hidden'.

NavList can then be

<NavList style={{visibility: ${this.state.visibility} }}>

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.