React Spring Imperatives & Refs
Last Updated :
25 Jul, 2024
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 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.
Imperatives and Refs: Imperatives are a small set of the property and refs are used to return a mutable ref object.
Syntax of Imperative API:
api({
//property
})
Syntax of Refs:
useSpringRef()
Steps to create React App are:
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: It will look like the following.
Example1: In the below code, we will make use of the above syntax to demonstrate the use of the Imperative API.
GFG.jsx
JavaScript
import { useSpring, animated } from 'react-spring'
import React, { useEffect } from 'react';
function BackwardsCompatability() {
const [styles, api] = useSpring(() => ({
from: { x: -50, opacity: 1 },
}))
useEffect(() => {
api({
x: 150,
opacity: 1,
loop: { reverse: true },
})
})
return (
<animated.div
style={{
width: 80,
height: 80,
backgroundColor: 'lightgreen',
borderRadius: 16,
boxShadow: 'rgb(0,0,0,0.44) 0px 5px 5px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'green',
margin: 250,
...styles,
}} >GFG</animated.div>
)
}
export default BackwardsCompatability;
App.js
JavaScript
import React from 'react'
import GFG from './GFG'
function App() {
console.log('hello')
return (
<>
<GFG />
</>
);
}
export default App;
Step to run the application: Run the following command:
npm start
Output:
Example2: In the below code, we will make use of the above syntax to demonstrate the use of the Refs.
GFG.jsx
JavaScript
import { useSpring, animated } from 'react-spring'
import React, { useEffect } from 'react';
function BackwardsCompatability() {
const [styles, api] = useSpring(() => ({
from: { x: -50, opacity: 1 },
}))
useEffect(() => {
api({
x: 150,
opacity: 1,
loop: true,
})
})
return (
<animated.div
style={{
width: 80,
height: 80,
backgroundColor: 'lightgreen',
borderRadius: 16,
boxShadow: 'rgb(0,0,0,0.44) 0px 5px 5px',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'green',
margin: 250,
...styles,
}} >GFG</animated.div>
)
}
export default BackwardsCompatability;
App.js
JavaScript
import React from 'react'
import GFG from './GFG'
function App() {
console.log('hello')
return (
<>
<GFG />
</>
);
}
export default App;
Output:
Reference: https://react-spring.dev/common/imperatives-and-refs#imperative-api
Similar Reads
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 as it will show the available balance the user ha
6 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 as it will show the available balance the user ha
6 min read
What are Refs used for in React Native ? Refs can be used to access DOM nodes or React Native components created in the render method.We can use Refs in react native with the help of the useRef hook which returns a ref object. The object has a .current property through which the React native components can be accessed. The returned object
5 min read
How to create refs in React JS? React JS, a powerful and declarative JavaScript library for building user interfaces, provides a feature known as "refs" that allows developers to interact with the underlying DOM elements directly. Refs are generally used for the following purposes:Managing focus, text selection, or media playback.
4 min read
ReactJS Props Reference In React, one of the key concepts that make it flexible and powerful is props. Props, short for "properties", are used to pass data from one component to another. They enable React components to be dynamic and reusable, allowing data to flow down the component tree.In this article, weâll take a clos
2 min read
React Rebass Link Component React Rebass is a front-end framework that was designed keeping react in mind. In this article, we will know how to use Link Component in React Rebass. The Link is an important component that is required in each development. So to create a Link component we can import the Rebass Link component.The l
1 min read