Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

toggle classname onclick react

function MyComponent (props) {
  const [isActive, setActive] = useState(false);

  const toggleClass = () => {
    setActive(!isActive);
  };

  return (
    <div 
      className={isActive ? 'your_className': null} 
      onClick={toggleClass} 
    >
      <p>my text</p>
    </div>
   );
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #toggle #classname #onclick #react
ADD COMMENT
Topic
Name
1+5 =