score:3
Accepted answer
import { useEffect, useState, useRef } from "react";
import "./styles.css";
export default function App() {
const [loading, setLoading] = useState(true);
const imageRef = useRef();
useEffect(() => {
if (loading === true) {
setTimeout(() => {
if (loading === true) {
setLoading(false);
}
}, 1000);
}
}, [loading, setLoading]);
useEffect(() => {
if (loading === true) {
imageRef.current.src =
"https://c.tenor.com/I6kN-6X7nhAAAAAj/loading-buffering.gif";
} else {
imageRef.current.src =
"https://post.healthline.com/wp-content/uploads/2020/08/3180-Pug_green_grass-732x549-thumbnail-732x549.jpg";
}
}, [loading, imageRef]);
return (
<div className="App">
<img ref={imageRef} alt="okay" />
</div>
);
}
score:0
return (
{"your true/false condition" ? <img src={image1}/> : <img src={image2}/>}
)
score:1
import {useState} from 'react';
const [source,setSource] = useState("");
if(reasultl === 'true'){
setSource(igo}
}else{
setSource(placeholder)
}
<img id="indigo" src={source} height="134" width="130" />
Source: stackoverflow.com
Related Query
- How to use Refs to change image src in React js
- how to change image src using props with styled component and react
- How to use refs in React with Typescript
- How to give Image src dynamically in react js?
- How I can use refs to change styling class in ReactJS?
- How to use refs in react through react-redux , withRouter?
- How to use React refs to focus a Redux Form field?
- How to use React Native Animate to change view flex from 0 to 1
- How to use React Router with Material UI Drawer to change content on click?
- How to use Refs with Material-ui 4, React 16.8, TypeScript 3.5
- How to use browser back/forward buttons to navigate React state change history
- How to import svg files and use them for src in image
- React : how to load image outside src and public folders
- How to change image on button click with React Hooks?
- How to use refs in React Higher-Order Component (HOC)
- How to use function in callback of image onload event in react js?
- How to use React refs with new Typescript strictPropertyInitialization option?
- how to use button as link in material-ui react with out underline and change color text?
- How to use the imported image in React within a function inside a class?
- How Can I Change Next.Js Image Component src with animation?
- How to use onChange with react hook on react select to change data base on user select
- How Can I use the returned string (new feature of React v16.0.0) in the source tag of an image ex: <img src='returnedString' />?
- How can I use setInterval() to automatically change slides on this React Spring project
- How to change image and background color every x seconds. React
- How do I use the useState hook to change the color of my react icons to blue?
- React how to use refs to focus/blur?
- how to use image in react lazy fallback
- How to add listener on change Object which already defined with @observable decorator in MobX to use in React
- How to properly use refs in React functional components
- How to use template string to create link from props and render image in React component
More Query from same tag
- Form submit after multiple tasks are completed in React
- Why am i getting ENOENT errors while starting my development server?
- How to make a Chat room with react and firebase?
- React "useViewBox" hook for SVG viewBox failing ESLint rule exhaustive-deps
- How to re-render a variable using useEffect on a new route change in react
- Using onClick in react-virtulized Grid cell/row
- Can't use default colors inside extendTheme (ChakraUI)
- Editing an an array of object states
- Displaying one List at a time on Clicking button
- how to change icon size in googleMap ,react integration
- Is there a generic event that is fired when a browser tab or window closes?
- Using context Api with useReducer hook in next.js gives undifined back when useContext is called
- React not overwriting state
- Difference between dates, rounded result to nearest minute
- How to customize Material-UI and styled-components props-based styles
- How to create a copy of an array from an API to sort it?
- Folder structure for React and Django REST
- Image is not loading into react
- Reach router changes URL, does not update view
- React - Run a function on useEffect and on element click?
- I'm getting an "Uncaught TypeError: Cannot read properties of undefined (reading 'push')" with react-router-dom v6
- Casperjs google auth popup
- How to put image as border in CSS in React JS
- How to change css of class default in antd?
- Passing properties to react component.Getting Error - Property 'firstName' does not exist on type 'Readonly<{}>'
- How can I change the value of a context without it resetting after a page change in Ionic React?
- Why my css class does not load?
- Why is my React component library not tree-shakable?
- Unable to clear input field automatically after clicking on submit (React)
- React - Material UI grid items center issue