6

I am struggling to convert my normal jQuery code in to React JS (I'm new to React).

I have the following code:

$(".add").click(function () {
    $("nav").addClass("show");
});
$(".remove").click(function () {
    $("nav").removeClass("show");
});
$(".toggle").click(function () {
    $("nav").toggleClass("show");
});
nav {
  width: 250px;
  height: 150px;
  background: #eee;
  padding: 15px;
  margin-top: 15px;
}
nav.show {
  background: red;
  color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<button class="add">Add</button>
<button class="remove">Remove</button>
<button class="toggle">Toggle</button>
<nav>Navigation menu</nav>

Tried references seems that only add/remove class for the same element:

https://stackoverflow.com/a/42630743/6191987

How to add or remove a className on event in ReactJS?

So, how can I convert or create the same jQuery methods to ReactJS?

2
  • Please show us what have you attempted in React so far. Commented Aug 18, 2020 at 9:16
  • @SibasishMohanty Already added references, but that wont't help for my case. I mentioned I am new to React JS Commented Aug 18, 2020 at 9:21

2 Answers 2

8
  1. Use the state that keeps the track of whether to show the nav or not.
  2. Use a class name in the react code that corresponds to the state.
  3. React uses "className" instead of "class" since "class" is already a reserved word in javascript.

You could check this sandbox link for implementation

function App() {
  const [show, setShow] = React.useState();

  return (
    <div className="App">
      <button className="add" onClick={() => setShow(true)}>
        Add
      </button>
      <button className="remove" onClick={() => setShow(false)}>
        Remove
      </button>
      <button className="toggle" onClick={() => setShow(!show)}>
        Toggle
      </button>
      <nav className={show ? "show" : ""}>Navigation menu</nav>
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

4 Comments

<nav class="menu"> has already a class "menu", so how can I write this: className={show ? "show" : ""} (need existing class to the code)
@vishnu you could use template strings. Since classNames are strings, you could concatenate them. <nav className={nav ${show ? "show" : ""}}>Navigation menu</nav>
@vishnu I have also made changes to the code sandbox link. You could check there as well.
this works for me.. I appreciate your great support
5

You could combine the use useRef for nav and manipulate the ref (accessing nav's DOM) with event handlers for each button

import React from "react";
import "./styles.css";

export default function App() {
  const navRef = React.useRef(null);

  const onAddClick = (e) => {
    navRef.current.classList.add("show");
  };

  const onRemoveClick = (e) => {
    navRef.current.classList.remove("show");
  };

  const onToggleClick = (e) => {
    navRef.current.classList.toggle("show");
  };

  return (
    <div className="App">
      <button onClick={onAddClick}>Add</button>
      <button onClick={onRemoveClick}>Remove</button>
      <button onClick={onToggleClick}>Toggle</button>
      <nav ref={navRef}>Navigation menu</nav>
    </div>
  );
}
.App {
  font-family: sans-serif;
}

nav {
  width: 250px;
  height: 150px;
  background: #eee;
  padding: 15px;
  margin-top: 15px;
}

nav.show {
  background: red;
  color: white;
}

Codesanbox link for demo

Edit objective-framework-lpl9e

Reference:

12 Comments

Shows a Type error: "Cannot read property 'classList' of null"
@vishnu have you tried like what is implemented in codesandbox link?
@vishnu could you send me the screenshot of your code through imgur link?
@vishnu thanks but I meant the code, not the error, it could be better if I could glance at the part of the code to check what was going on
|

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.