score:3
For complex animations, you can use https://github.com/chenglou/react-motion which can learnt from https://egghead.io/playlists/react-react-animation-using-react-motion and https://egghead.io/lessons/react-react-motion-introduction-to-the-spring-component
For simple ones, I would rather add and remove classes from element to enable the animation and reset it.
And if I am understanding it correctly, you are using jQuery inside react. Well to update the CSS, you should not use it inside react.
There should be more strong reasoning to use jQuery in react because updating inline css is very simple in react.
Following is the way I would attempt to do it:
// React Component
class Player extends React.Component {
constructor(props) {
super(props);
this.state = {
direction: null,
timeout: null
};
}
attack(e) {
//following line can be used to identify multiple targets
//let target = (e.target);
if (this.state.timeout !== null) {
window.clearTimeout(this.state.timeout)
}
let timeout = setTimeout(() => {
this.setState({
direction: null,
timeout: null
});
}, 8000)
let direction = null;
if (e.ctrlKey) {
direction = 'anticlockwise'
} else {
direction = 'clockwise'
}
if (this.state.direction !== direction) {
this.setState({
direction, timeout
});
}
}
render() {
let classNames = ["player", "main"];
let styles = {}
if (this.state.direction !== null) {
if (this.state.direction === 'clockwise')
styles.zoom = 0.8
else
styles.zoom = 1.2
classNames.push(this.state.direction)
}
return ( < div onClick = {
this.attack.bind(this)
}
style={styles}
className = {
classNames.join(' ')
} > {
this.props.name
} < /div>
);
}
}
ReactDOM.render(<Player / > , document.getElementById('game'));
.player {
width: 100px;
height: 100px;
margin: 50px;
zoom: 1;
transition: all linear 0.3s;
display: inline-block;
background-color: #ccc;
animation-timing-function: linear;
}
.clockwise {
animation: clockwise 5s infinite;
}
.anticlockwise {
animation: anticlockwise 5s infinite;
}
@keyframes clockwise {
0% {
transform: rotate(0deg)
}
50% {
transform: rotate(180deg)
}
100% {
transform: rotate(360deg)
}
}
@keyframes anticlockwise {
0% {
transform: rotate(360deg)
}
50% {
transform: rotate(180deg)
}
100% {
transform: rotate(0deg)
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="game"></div>
Source: stackoverflow.com
Related Query
- How do you cancel and reset css animations in react?
- How Do you reset the CSS in React if you're doing styled components?
- How can you use CSS modules together with Typescript, React and Webpack?
- How to define css variables in style attribute in React and typescript
- How to import CSS modules with Typescript, React and Webpack
- How to apply CSS and Styling to a React component
- How do you center a div element in react w/out external css file
- How to use vw and vh css with React Native
- How do you use CSS grid layouts with React effectively?
- How to load background images from css in React and NextJS
- How can I pass CSS classes to React children and ensure they will override the child’s local class?
- How to setup sublime 3 with react native so that when you click on the error and it jump to your code?
- How to write CSS / SCSS / PostCSS inside React .tsx file as JavaScript String in VSCode and WebStorm?
- How to determine the order of css in my create react app build with webpack and customize-cra?
- How do you embed and call an external javascript function in a react component?
- How do I apply CSS transitions (e.g. fade-ins) to a list of items in React so their animations are sequenced one after the other on render?
- How to make the pass a dynamic value to css class and cusmize in material UI in react
- Using RxJS and axios, how do you fetch data onClick in React component?
- I am trying to animate an object with css in react and change the animation (timing) dynamically, how can I set the style after animation end?
- How do you use Pseudo-classes in React using css modules?
- React App, How do you map and sort titles in an Api Array in alphabetical order
- React JS: How to load only page related js, css and html on production mode?
- How to communicate between React and CSS
- How to apply a CSS style in React based on condition and incorporate it with another class?
- How can you refactor and make react less bulky and more readable?
- Jest with React - How do you render an entire component and test it?
- How to individually change text color and font weight in the same div element using Tailwind CSS and React
- How to reset child component from parent using React Context and Hooks
- How to Resolve Memory Leak and Cancel All Subscriptions Error in React App?
- How to restrict formik validation when you click cancel button in react
More Query from same tag
- Why my on change event return empty on ie9?
- ejabberd get chat history of particular users
- Flipping a card, CSS animation for web app
- Why are the objects in setData not as array in data?
- Is it good practice for a react component to have returned value of null?
- Test of reducer returns the results which are not equal to expected
- Get text content from node in React
- Resolving multiple fetch request in parallel
- React : Ag Grid Pagination Server Side
- How to use multiple graphql query hooks in single function component in react?
- Loading data in react-multi-carousel using .map function gives 'length' of undefined error
- Avoiding automatic closing of a DIV
- React.JS Validating Two Inputs
- difference between state separately and together in react js
- react-router match query string
- How to implement a responsive layout with ReactJS?
- Binding Kendo widget values to a Kendo template/observable using React?
- Reactjs application Azure deployment getting failed
- Failing to apply material ui overrides in storybook in React app
- can't import IMG_VAR from './images/name.jpg' with babel / React / Webpack
- React event handler works without bind()
- LayoutAnimation expanding all lists on click of a button - React Native
- Can't add 'cursor: pointer' hover with JSS in React Typescript project
- converted class to hooks getting 'Uncaught RangeError: Maximum call stack size exceeded at getFloorplan '
- How do I redirect to another page on form submit?
- Console.log not working on Reactjs app
- React TypeError: Cannot use 'in' operator to search for 'length' in null
- Reactjs - Ordering div when resized
- React - Error: App(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null
- setState not triggering a re-render when data has been modified