How can I dynamically update className with React js?

babyFrontendDeveloper

I have a Collection component and when I click on a div in this component, I want to change both the className of the Collection component and the className of the first sibling component after the Collection component.

With UseState, I could only change the className of the component I was in.

My Collection.js looks like this:

const Collection = () => {
  const [toggleClass, setToggleClass] = useState(false);

  function handleClick() {
    setToggleClass((toggleClass) => !toggleClass);
  }

  let toggleClassCheck = toggleClass ? "passive" : "active";

  return (
    <React.Fragment>
      <div className={`step ${toggleClassCheck}`}>
        <div className="atrium">
          <span>Collection</span>
        </div>
        <div className="content">
          <div
            className="moreThenOne"
            area="collectionTitle"
            onClick={handleClick}
          >

Can anyone help me on how to do the process I mentioned above?

Nury Amandurdyev

I didn't quite understand what you want, but if you want to impact sibling of component "Collection" executing function inside of "Collection" you definitely should try "ref". Via useRef hook.

export const Collection = () => {
const [toggleClass, setToggleClass] = useState(false);
const divRef = useRef(null);

function handleClick() {
  divRef.current.nextSibling.classList.toggle('active');
  divRef.current.nextSibling.classList.toggle('passive');
  setToggleClass((toggleClass) => !toggleClass);
}

let toggleClassCheck = toggleClass ? 'passive' : 'active';

return (
<>
  <div className={`step ${toggleClassCheck}`} ref={divRef}>
    <div className="atrium">
      <span>Collection</span>
    </div>
    <div className="content">
      <div className="moreThenOne" onClick={handleClick}>
        CLICK ZONE
      </div>
    </div>
  </div>
</>
);
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React Js how can i update the State after changes? With componentDidUpdate?

How can I dynamically update bindings in MvvmCross?

How can I deal with a long className in React JSX?

How can I add multiple className's to react component?

how can i add classname body scroll down react?

How can I create reusable function for this React code(active className)

How to dynamically change className of a component in React?

How can I get classname which has been created by js?

How can i Dynamically Update the React Helmet Value when state changes

How can I dynamically attach an event in React?

How can I dynamically create a Route in React?

How can i create input text fields dynamically in react js - JSX?

How can I dynamically loading external configuration settings in a React JS app?

Adding a className dynamically in React.js with Tailwind.css

CSS transition not working when I set a className dynamically in React

How can I update an angular directive attribute dynamically?

How can I dynamically update the array within a struct?

How can I update Gtk.ListBox dynamically?

How can I dynamically update the JSON-LD script with Ajax?

How can I update all values in an array dynamically in jsonb column?

How can I dynamically update values in an object state?

Snowflake - how can I update user roles dynamically with a cursor?

How can I get the classname at runtime, but only the classname?

How to set className dynamically?

How can i update values for all keys in nested object at once in react js?

How can I change divIcon className on click?

How can I override css style with className?

How can i change classname condition with map?

using css modules in react how can I pass a className as a prop to a component