Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add animated gifs to react web apps

I'm trying to define a transition state for my react web application when I'm publishing data to backend.

I want to show an animated gif in my render method.

So I import the gif image (like this enter image description herehttps://media.giphy.com/media/3oEjI6SIIHBdRxXI40/giphy.gif )

using a simple import

import logo from '../assets/load.gif'

and add it to my render

like:

<img src={logo} alt="loading..." />

but I get an error in my react-dev server terminal

unknown character

How to add animated gif's to a plain react SPA.

like image 848
Probosckie Avatar asked Jun 05 '17 14:06

Probosckie


People also ask

How do you add a GIF to a react website?

To render an animated gif in a React component:Import the gif as import myGif from './path-to-my-gif. gif' . Add an img element that shows the gif, e.g. <img src={myGif} alt="" /> .

How do you use Giphy in react?

Write the following code in your App. js file: import { GiphyFetch } from '@giphy/js-fetch-api' import {useState} from 'react' import TextList from './components/TextList' import Error from './components/Error' import './App. css'; const giphy = new GiphyFetch(process.

How do I add a GIF to my HTML website?

It is in GIF format i.e. Graphics Interchange Format file. We need to use the <image> tag with the src attribute to add an animated image in HTML. The src attribute adds the URL of the image (file destination). Also, we can set the height and width of the image using the height and width attribute.


2 Answers

You can also use require('path') method

The require() method fetches the images and converts it to data URI format Read about Data URI here

<img src={require('../assets/load.gif')} alt="loading..." />
like image 194
Bharath Avatar answered Sep 18 '22 16:09

Bharath


I had a version that used a URL however, it stopped working. I found a site that you can download the spinner as a GIF, then I put that .gif into my images folder. From there you can use:

import logo from '../assets/load.gif'

&

<img src={logo} alt="loading..." />
like image 21
Alex Avatar answered Sep 19 '22 16:09

Alex