Search
 
SCRIPT & CODE EXAMPLE
 

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>
   );
}
Comment

onclick toggle class react

import React, { useState } from "react";import "./styles.css";

export default function App() {
  const [isActive, setActive] = useState("false");
  const handleToggle = () => {
    setActive(!isActive);  };
  return (
    <div className={isActive ? "app" : null}>      <h1>Hello react</h1>
      <button onClick={handleToggle}>Toggle class</button>    </div>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Javascript convert html entity to string 
Javascript :: foreach loop in jquery 
Javascript :: how to iterate through an array in javascript 
Javascript :: link regex 
Javascript :: javascript execute string code 
Javascript :: s3.getobject nodejs example async await 
Javascript :: select add option javascript 
Javascript :: check if array has same values javascript 
Javascript :: add variable numerically in javascript 
Javascript :: Mongoose - populate nested array 
Javascript :: how to parse json in java 
Javascript :: axios download excel file 
Javascript :: get element with one or another class 
Javascript :: typescript css variables 
Javascript :: how to return json response in flask 
Javascript :: loadash sort by order of another array 
Javascript :: vue.js function to always uppercase when the client input lowercase 
Javascript :: javascript change div order 
Javascript :: install react app 
Javascript :: $(document).ready, window.onload 
Javascript :: react deep copy 
Javascript :: remove all white space from text javascript 
Javascript :: angular create guard 
Javascript :: xhr post send 
Javascript :: compare and filter two array of object 
Javascript :: laravel csrf token ajax post 
Javascript :: how to generate random string in javascript 
Javascript :: sum numbers recursively js 
Javascript :: js get date in ms 
Javascript :: content editable vuejs 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =