1

Learning React and trying to simply toggle a css class on body tag when a element in my component is clicked. I can't seem to find a good example on how to do it.

Here is how I would do it using Jquery.

function initNav() {
$(".hmbWrapper").click(function() {
    $('body').toggleClass('mobileNavActive');
});
}

What is the correct approach in React?

1
  • You can do it using setState, the right question here is how to toggle class using vanilla JavaScript. Commented Apr 29, 2018 at 16:40

4 Answers 4

3

You can do something like this, assuming you have a <div id="test">something</div> in the index.html file.

class App extends Component {
  toggleClass = () => {
    const oldClassName = document.getElementById('test').className;
    const newClassName = oldClassName === 'red' ? 'blue' : 'red'
    document.getElementById('test').className = newClassName
  }

  render() {
    return (
      <div>
        <p>
          click toggle to change colors below
        </p>
        <button onClick={this.toggleClass}>toggle</button>
      </div>
    );
  }
}

Working example here.

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

3 Comments

How would I target a parent element that is not part of my component? I'm trying to toggle a class on body tag.
Thanks! I think this is what I was looking for
Actually this is good for adding inline styles but I'm trying to add a css class on body tag. I need a class because I'm targeting many other child elements with css depending on this body class.
3
class App extends Component {
 toggleClass = (event) => {
   event.target.classList.toggle('selected')
}

render() {
 return (
  <div>
    <button onClick={this.toggleClass}>toggle</button>
  </div>
);
}}

2 Comments

Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
Isn’t this toggeling the classname of the button itself instead of the body tag like asked?
1

if the toggled element is not part of React, you can do it in old fashion way using pure javascript.

class MyComponent extends React.Component {
  toggle = () => document.body.classList.toggle('mobileNavActive');

  render() {
    return <button onClick={this.toggle}>toggle</button>;
  }
}

1 Comment

This should be the right answer. Addressing classname toggle on body tag like asked and straight to the point.
0
const myComponent = () => {
    [state, setState] = useState('red');
    const colorHandler = () => {
        setState(state === 'red' ? 'blue' : 'red');
    }
    return (<button onClick={colorHandler}>toggle</button>);
}

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.