I want to have a react component flip over when a user clicks on the DOM element. I see some documentation about their animation mixin but it looks to be set up for "enter" and "leave" events. What is the best way to do this in response to some user input and be notified when the animation starts and completes? Currently I have a list item and I want it to flip over an show a few buttons like delete, edit, save. Perhaps I missed something in the docs.
animation mixin
http://facebook.github.io/react/docs/animation.html
You can use CSS classes to animate in React import React, { useState } from 'react'; import classNames from 'classnames'; import styles from './App. module. css'; const App = () => { const [animate, setAnimate] = useState(false); const handleClick = () => setAnimate(!
The React onClick event handler enables you to call a function and trigger an action when a user clicks an element, such as a button, in your app. Event names are written in camelCase, so the onclick event is written as onClick in a React app. In addition, React event handlers appear inside curly braces.
Upon clicks you can update the state, add a class and record the animationend
event.
class ClickMe extends React.Component { constructor(props) { super(props) this.state = { fade: false } } render() { const fade = this.state.fade return ( <button ref='button' onClick={() => this.setState({ fade: true })} onAnimationEnd={() => this.setState({ fade: false })} className={fade ? 'fade' : ''}> Click me! </button> ) } }
See the plnkr: https://next.plnkr.co/edit/gbt0W4SQhnZILlmQ?open=Hello.js&deferRun=1&preview
Edit: Updated to reflect current React, which supports animationend events.
React uses synthetic events, which includes animation events. Documention found here: https://reactjs.org/docs/events.html#animation-events. I updated the accepted answer below:
class ClickMe extends React.Component { state = {fade: false}; render () { const {fade} = this.state; return ( <button onClick={() => this.setState({fade: true})} onAnimationEnd={() => this.setState({fade: false})} className={fade ? 'fade' : ''}> Click me! </button> ) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With