Can a placeholder be created for Next.js Image without using blur effect?

Currently, I am utilizing Next.js in my projects. The Next.js Image component includes placeholder and blurDataURL attributes which work seamlessly together. However, when attempting to incorporate a custom SVG placeholder (converted to base64) the result is blurred.

I have made numerous attempts to locate information on how to disable the blur effect without success. If anyone has any insights or suggestions, it would be greatly appreciated! Thank you in advance!

Answer №1

Here is how I implement a placeholder for loading images in my code snippet

'using client'

import { useCallback, useMemo, useState } from 'react';
import Image, { ImageProps } from 'next/image';

import loadingPlaceholder from '@/assets/images/loading-placeholder.svg';
import errorPlaceholder from '@/assets/images/error-placeholder.svg';

const ImageWithPlaceHolder = ({ src, alt, ...otherProps }: ImageProps) => {
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(false);

  const startLoading = useCallback(() => setLoading(true), []);
  const completeLoading = useCallback(() => setLoading(false), []);

  const errorHandler = useCallback(() => setError(true), []);

  const internalSrc = useMemo(
    () => (error ? errorPlaceholder : loading ? loadingPlaceholder : src),
    [error, loading, src],
  );

  return (
    <Image
      src={internalSrc}
      alt={alt}
      {...otherProps}
      onError={errorHandler}
      onLoadStart={startLoading}
      onLoadingComplete={completeLoading}
    />
  );
};

export default ImageWithPlaceHolder;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Python's Selenium execute script does not support setTimeout function

Hey there! I was attempting to pause the Selenium execution for a few seconds in order to wait for a Modal popup to appear. Unfortunately, using time.sleep(5) did not work with PhantomJS (apparently, PhantomJS does not support sleep function). So, I decide ...

Is there a way for me to import a variable from one file and use require() to access it in another file?

This is my current folder structure: collegesapp -- |-- node_modules -- express | -- connect | -- jade | -- passport |-- routes -- routes.js ...

Steps to make the placeholder in an MUI TextField component move to the top of the box instead of staying within the border:

I'm working on styling a text field in MUI to achieve the look shown in the image below: https://i.sstatic.net/JHhpf.png However, my current implementation looks like this: https://i.sstatic.net/5N7hH.png Currently, when I click inside the text fi ...

What is preventing MenuItemLink from being displayed in the menu?

I have created a unique page for users to purchase subscriptions, but I am having trouble accessing that page because the button is not appearing in the menu. I followed the steps outlined in the official guide, but only the dashboard and resources buttons ...

ffmpeg is successfully converting less than half of my images into a video

Utilizing ffmpeg to convert a series of images into a timelapse video has been seamless when executed through the command line. ffmpeg -r 3 -i /var/folders/qj/n910kwdj4gvbmy_z2ffc5lcc0000gp/T/tmp-22129yvIsrbso4TEu/image%03d.jpg -s hd1080 -vcodec libx264 t ...

How can we effectively use mongoose populate in our codebase?

Hey there, I'm a newbie when it comes to nodejs and mongoose, and I could really use some assistance with mongoose populate. Can someone please help me understand this better? Thanks in Advance! Here are the schemas I'm working with: PropertySch ...

Typescript is outputting the error message: "cannot be assigned to the type 'IntrinsicAttributes & { children?: ReactNode; }'"

While there has been extensive discussion on this topic, I am unable to get any other solutions from SO to work, or I am struggling to implement them. I am completely new to TypeScript, and here is what I have: import faker from "faker"; import React fro ...

An internal server error has occurred within the asp.net framework, resulting in

Seeking Solution: Currently, I am in the process of developing an application where I have implemented an HTML select list to choose a category. Using AJAX web method, I am trying to retrieve items and their respective images based on the selected category ...

Discover the DOM events present in an HTML response

When sending multiple HTTP requests to a server and receiving a HTML + JS response, I need to determine which responses trigger JS alerts or DOM-based events. Node.js is being used to send the requests. Especially in cases where there is local JS on the c ...

Setting up the customized filename for precompiled Handlebars templates

When compiling Handlebars templates with the NPM package, is there a way to manually adjust the name/index that is generated? In my experience using Handlebars in various environments like Rails, NodeJS, and PHP, I've observed that the generated temp ...

Is it possible to prevent the fade out of the image when hovering over the text after hovering over the div or image, causing the text to fade in?

Using React and CSS. I have set up my application to display a faded image on hover, with text that fades in over it. However, I am facing an issue where touching the text with my cursor removes the fade effect. Does anyone have a solution for preventing ...

How can one retrieve the set-cookie value in a <meta http-equiv> tag using Node.js?

When working with Node.js, I am encountering a scenario where I need to detect instances of a cookie being set in a response so that I can make changes to it. There are two ways in which cookies are being set: Through the use of the set-cookie HTTP heade ...

What is the best way to eliminate excess space on the right side in Bootstrap 4 while in mobile view?

I'm struggling to understand why this layout is not responsive on mobile devices, triggering a bottom scroll bar at around 616px. I'm looking for a solution to hide the scroll bar at least until 414px for iPhones and other smartphones. I've ...

Looking to pass the `Item Index` to functions within v-list-item-action in Vuetify?

Is there a way to pass the item index as a parameter to the function within the v-list-item-action element? Thank you in advance! <v-list-item v-for="(layer, i) in layers" :key="i"> <template v-slot="{ item, index }& ...

Error: File or directory not found, unable to locate BUILD_ID

What is the next step? Error: ENOENT: The file or directory 'C:\Janani\ticket-app.next\BUILD_ID' does not exist { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'C:\Janani\ticket-app\. ...

"Encountered a type error with the authorization from the credentials provider

Challenge I find myself utilizing a lone CredentialsProvider in next-auth, but grappling with the approach to managing async authorize() alongside a customized user interface. The portrayal of the user interface within types/next-auth.d.ts reads as follo ...

Switch up the image URL with a click of a button

On my asp.net page, I have an Image control that displays myimage.png when the page loads. The requirement is to browse for an image using a File upload control and display an immediate preview in the Image control upon clicking the Upload button. When the ...

3.2+ Moodle communication and connections

I am facing a challenge with creating a new div @type@ in /message/templates/message_area_contact.mustache This is the code snippet: ... <div class="name"> {{fullname}} <div style="font-style:italic; font-weight:100;">{ ...

"Error encountered: 'require' is not defined in the bundled JS file for

Recently, I decided to try my hand at Django and ReactJS. While attempting to compile a simple JSX code to JS, I followed this tutorial: . However, I encountered an error that prevented it from working. I then resorted to using npm run dev to compile the c ...

Changing the value in sessionStorage does not trigger the onChange event (Next.js)

When I use a custom hook to load data from session storage into an input field, I noticed that the onChange() function doesn't trigger if I delete the entire content of the input. However, it works fine if I add or delete just one character. This issu ...