React Spring Events as Objects
Last Updated :
24 Jul, 2024
In this article, we will learn how to use Events as Objects using React Spring. React spring is an animation library that makes animating UI elements simple. It is based on spring physics which helps it to achieve a natural look and feel. It is different from other animation libraries where someone has to deal with curves, easing, and time durations, all of which are in sync with each other.
Platforms: React spring is a cross-platform library, it supports react, react-native, web, and many more platforms. It also has support for all browsers.
Events as Objects: It is used to create an object with the events like onRest.
Syntax:
useSpring({
from: { x: 0, y: 0 },
onRest: {
x: () => console.log('x.onRest'),
y: () => console.log('y.onRest'),
},
})
Steps to create an app in react.
Step 1: Create a new application using the following command.
npx create-react-app reactspringdemo
Step 2: Now move the created project folder using the following command.
cd reactspringdemo
Step 3: Install the react spring library.
npm install react-spring
Project structure: Import the app folder into the application and after that, the structure will look like this.
Example1: In the below code, we will make use of the above syntax to demonstrate the use of the Events as Objects.
JavaScript
import React from 'react';
import { useSpring, animated } from 'react-spring'
function OnRest() {
const styles =
useSpring({
from: { x: 0, y: 0 },
onRest: {
x: () => console.log('x.onRest'),
y: () => console.log('y.onRest'),
},
})
return (
<animated.div
style={{
width: 80,
margin: 221,
height: 80,
backgroundColor: 'green',
borderRadius: 16,
...styles,
}}
/>
)
}
export default OnRest;
JavaScript
import React from 'react'
import GFG from './GFG'
function App() {
console.log('hello')
return (
<>
<GFG />
</>
);
}
export default App;
Step to run the Application: Open the terminal and type the following command.
npm start
Output:
Example2: In the below code, we have used a loop to animate an object.
JavaScript
import React from 'react';
import { useSpring, animated } from 'react-spring'
function LoopTrue() {
const styles = useSpring({
loop: true,
from: { rotateZ: 0 },
to: { rotateZ: 180 },
})
return (
<animated.div
style={{
width: 80,
height: 80,
backgroundColor: 'green',
borderRadius: 16,
margin: 100,
...styles,
}}
/>
)
}
export default LoopTrue;
JavaScript
import React from 'react'
import GFG from './GFG'
function App() {
console.log('hello')
return (
<>
<GFG />
</>
);
}
export default App;
Step to run the Application: Open the terminal and type the following command:
npm start
Output:
Reference: https://react-spring.dev/common/props#events-as-functions
Similar Reads
React Spring Overview of Props In this article, we will learn the overview of props in React Spring. React spring is an animation library that makes animating UI elements simple. It is based on spring physics which helps it to achieve a natural look and feel. It is different from other animation libraries where someone has to dea
3 min read
Event Calendar using React The Event calender using react basically displays a calendar with the additional feature of adding an event on a particular date by selecting it. User can also delete the event if he/she wants to delete it. All this logic of event creation and deletion is implemented using JSX. This project is usefu
7 min read
React onCut Event React onCut Clipboard event is an event handler event, which detects the cut process in the browser. When the user starts cutting data through the shortcut key (CTRL + X) or the cut button present in the menu, this even automatically fires, and the function passed to it will call itself automaticall
2 min read
React Spring Inherited Props In this article, we will learn about Inherited Props in React Spring. React spring is an animation library that makes animating UI elements simple. It is based on spring physics which helps it to achieve a natural look and feel. It is different from other animation libraries where someone has to dea
3 min read
ReactJS Rendering Elements In this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are different from DOM elements as React elements are simple JavaScript objects and are effic
3 min read
React Spring Compatible Props In this article, we will learn how we can use compatible props using React Spring. React spring is an animation library that makes animating UI elements simple. It is based on spring physics which helps it to achieve a natural look and feel. It is different from other animation libraries where someo
3 min read
React Spring Imperatives & Refs In this article, we will learn how Imperatives and Refs work. React spring is an animation library that makes animating UI elements simple. It is based on spring physics which helps it to achieve a natural look and feel. It is different from other animation libraries where someone has to deal with c
3 min read
Expense Tracker using React The Expense Tracker project is a web application developed using React. Its main purpose is to assist users in keeping track of their expenses. With this app, users can easily add, and delete expenses to view a summary of their spending habits as well and it will show the available balance the user
7 min read
ReactJS Methods as Props In this article, we will learn about props and passing methods as props. We will also discuss how we can use the child components to pass data to parent components using methods as props.What are props?We know that everything in ReactJS is a component and to pass in data to these components, props a
3 min read
Emoji Picker App using React In this article, we will create an Interactive Emoji Application using ReactJS Framework. This developed project basically implements functional components and manages the state accordingly.The developed Application allows users to explore a wide range of emojis and select them for their communicati
4 min read