How to use MoJS with React
Tags: react, html, css, javascript
Published: 2020-05-09 03:30:57


![How to use MoJS with React](https://i2.wp.com/blogreact.com/wp-content/uploads/2020/05/cowboy.gif?w=750&ssl=1)

Creating beautiful animations is one of the most difficult task for a Frontend developer. The solution to this problem is to use some super easy to use Library like MoJS. In this article we are going to learn how to use MoJS with React to create super awesome animations.

## What is  [MoJS](https://mojs.github.io/)  ?

MoJS is nothing but a JavaScript based library which is used to create cool animations. The reason why MoJS is so popular is that because it is super fast, have retina ready capability and open source.

MoJS provides full control to developer to control the animation in any way they want due to that doors for creativity also opens for creative people. MoJS have some built in components to start animations and apart from that you can also create custom animations.

## Use MoJS with React

To use MoJS with React we need to install the package first.

```
npm i @mojs/core --save
```

After the installation we need to import our mojs in our file.

```
import mojs from '@mojs/core'
```

For displaying the animation we gonna need an empty div.

```
 render() {
    return <div id="test" />;
  }
```

In the componentDidMount we can put our animation code.

```
  componentDidMount() {
    new mojs.Shape({
      parent: "#test",
      shape: "circle",
      fill: { "#F64040": "#FC46AD" },
      radius: { 20: 80 },
      duration: 2000,
      isYoyo: true,
      isShowStart: true,
      easing: "elastic.inout",
      repeat: 1
    });
  }
```

Our full component will gonna look like this

```
import React, { Component } from "react";
import mojs from "@mojs/core";
import "./styles.css";

export default class App extends Component {
  componentDidMount() {
    new mojs.Shape({
      parent: "#test",
      shape: "circle",
      fill: { "#F64040": "#FC46AD" },
      radius: { 20: 80 },
      duration: 2000,
      isYoyo: true,
      isShowStart: true,
      easing: "elastic.inout",
      repeat: 1
    });
  }
  render() {
    return <div id="test" />;
  }
}

```

You will be able to see this UI in your webpage now.

![How to use MoJS with React](https://i2.wp.com/blogreact.com/wp-content/uploads/2020/05/image-2.png?w=750&ssl=1)

Now you will gonna notice its not showing the animations yet. To show the animations we need to trigger play function of the animation.

We can store our animation in a variable.

Like this

```
  const animation = new mojs.Shape({
      parent: "#test",
      shape: "circle",
      fill: { "#F64040": "#FC46AD" },
      radius: { 20: 80 },
      duration: 2000,
      isYoyo: true,
      isShowStart: true,
      easing: "elastic.inout",
      repeat: 1
    });
```

After that we can simple use play or replay function.

```
animation.play();
```

Our updated component will gonna look like this.

```
import React, { Component } from "react";
import mojs from "@mojs/core";
import "./styles.css";

export default class App extends Component {
  componentDidMount() {
    const animation = new mojs.Shape({
      parent: "#test",
      shape: "circle",
      fill: { "#F64040": "#FC46AD" },
      radius: { 20: 80 },
      duration: 2000,
      isYoyo: true,
      isShowStart: true,
      easing: "elastic.inout",
      repeat: 1
    });
    animation.play();
  }
  render() {
    return <div id="test" />;
  }
}

```

That’s the smallest example of how to use MoJS with React. Its upto you to try all animations or maybe create custom animations on your own.

[![Edit 7ib51](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/unruffled-frog-7ib51?file=/src/App.js)

I hope you have learned how to use MoJS with React feel free to comment.

[Top 5 animation libraries for React in 2020](https://blogreact.com/animation-libraries-react-2020/)