![]() |
VOOZH | about |
The react-motion package is a JavaScript animation library for the React applications which can be used for creating smooth and interactive animations. This uses the spring-based physics model, through which we can define animations with natural and dynamic movements. This library is mainly suited for complex UI animations and transitions.
In this article, we will explore this library with practical implementation in terms of an example.
Table of Content
Using the react-motion module in React JS includes the installation of the corresponding npm package in our React project. We will import the module in the component where we will apply the smooth and animative appearance and disappearance on the content block. The animation includes opacity, translation, scaling, and rotation effects, triggered by a button click.
Step 1: Create a React application using the following command:
npx create-react-app foldernameStep 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldernameStep 3: After creating the ReactJS application, Install the react-motion modules using the following command:
npm i react-motionThe updated dependencies in package.json file after installing required modules.
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-motion": "^0.5.2",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Code: This example adds the animation to the h1 and h3 Heading when the Button is clicked. Write the code in App.js file
Step to Run Application: Run the application using the following command from the root directory of the project:
npm startOutput: Now open your browser and go to http://localhost:3000/, you will see the following output: