Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add active class to li onclick react

const links = ['link1', 'link2', 'link3'];
const [active, setActive] = useState(null);

<ul>
  {links.map((link) => (
    <li className="nav-item">
      <a 
        href={`#${link}`} 
        className={`nav-link ${active == link && 'm-active'}`}
        onClick={() => setActive(link)}
      >{link}</a>
    </li>
  ))}
</ul>
Comment

add active class to button onclick react


class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      animate: false;
    }

    this.handleClick = this.handleClick.bind(this);
  }

  handleClick(e) {
    // modify the state, this will automatically recall render() below.
    this.setState((prevState) => {
      return { animate: !prevState.animate }
    });
  }

  render() {
    let animationClasses = (this.state.animate ? ' active': '');

    return (
      <div>
        <div className={`vote_card_hover${animationClasses}`}>
          <a>Test</a>
        </div>

        <div>
          <Button title="Toggle Animation" onClick={this.handleClick} />
        </div>
      </div>
    )
  }
}

Comment

add active class to button onclick react

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      animate: false;
    }

    this.handleClick = this.handleClick.bind(this);
  }

  handleClick(e) {
    // modify the state, this will automatically recall render() below.
    this.setState((prevState) => {
      return { animate: !prevState.animate }
    });
  }

  render() {
    let animationClasses = (this.state.animate ? ' active': '');

    return (
      <div>
        <div className={`vote_card_hover${animationClasses}`}>
          <a>Test</a>
        </div>

        <div>
          <Button title="Toggle Animation" onClick={this.handleClick} />
        </div>
      </div>
    )
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript add class to element 
Javascript :: electron jquery 
Javascript :: window.location.href 
Javascript :: convert string in hh:mm am/pm to date js 
Javascript :: javascript change url 
Javascript :: remove node modules command windows 
Javascript :: Warning: Failed child context type: Invalid child context `virtualizedCell.cellKey` of type 
Javascript :: javascript websocket 
Javascript :: js remove the last character from a string 
Javascript :: jquery validation manually trigger 
Javascript :: json multiple records 
Javascript :: join array of object name javascript 
Javascript :: iframe reload parent page 
Javascript :: javascript iterate through a map 
Javascript :: click anchor tag using jquery 
Javascript :: dictionary in javascript 
Javascript :: javascript array to table 
Javascript :: how to get url in react 
Javascript :: open new window chrome extension 
Javascript :: imdb-api 
Javascript :: javascript format number 2 digits 
Javascript :: generating random number in javascript 
Javascript :: deleting key of json object 
Javascript :: index of value in array 
Javascript :: Change the HTML of an element 
Javascript :: difference between statement and expression 
Javascript :: localstorage javascript array 
Javascript :: 12 hours to 24 hours javascript 
Javascript :: nodejs: read and write file: use fs and promise 
Javascript :: react native image auto height 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =