Coder Social home page Coder Social logo

Comments (11)

kikoanis avatar kikoanis commented on August 16, 2024 1

yeah I have the same behavior. You only need to check for it in order to set placeholder correctly.
<Image src={imageUrl} blurDataURL={blurDataurl} placeholder={blurDataUrl ? 'blur' : 'empty'} .... />

Or you could start blurDataUrl with a generic fallback blurDataUrl

from use-next-blurhash.

rushi avatar rushi commented on August 16, 2024 1

I had the same problem, I removed the code from the useEffect and it started working.

from use-next-blurhash.

ivansevillaa avatar ivansevillaa commented on August 16, 2024

are you using the hook inside the useEffect? could you provide me a codesandbox or something to take a look, please

from use-next-blurhash.

SimonVillage avatar SimonVillage commented on August 16, 2024

I will see if I can provide an example later.

The problem is that blurDataURL has to be set to something, otherwise nextjs will throw an error if placeholder = blur.
Now blurDataURL is only set once useEffect runs. Those nextjs is complaining that it's not available / undefined.

from use-next-blurhash.

ivansevillaa avatar ivansevillaa commented on August 16, 2024

Ahhh ok, and what about passing the prop blurData only if blurDataURL exists?

Another solution could be having a fallback, so if blurDataURL doesnยดt exist, you pass the fallback.

blurDataURL ={blurDataURL ? blurDataURL : fallbackURL}

Tell me if some of this works

from use-next-blurhash.

SimonVillage avatar SimonVillage commented on August 16, 2024

Ahhh ok, and what about passing the prop blurData only if blurDataURL exists?

then it would cause an error as placeholder = 'blur' requires blurDataURL

I assume that your other solution would work but I think that this package somehow should handle this and adding a default fallbackURL could cause flickering on render.

from use-next-blurhash.

ivansevillaa avatar ivansevillaa commented on August 16, 2024

Yes, you should pass the placeholder conditionally too.

Yes, I understand that. This package works well for static site generation. For the client side, I think flickering is inevitable if you get the URL in that mode.

Do you have an idea of how the package could handle that without providing a fallbackURL?

from use-next-blurhash.

kikoanis avatar kikoanis commented on August 16, 2024

I think blurDataUrl would be better it was set by a state hook. Otherwise, consumers of this hook would not know if it has changed or not.

import { useEffect, useState } from 'react';

import { decode } from 'blurhash';
import { createCanvas } from 'canvas';

export function useBlurData(blurHash: string, width = 160, height = 120, punch?: number): [string] {
    const [blurDataUrl, setBlurDateUrl] = useState('');

    useEffect(() => {
        const pixels = decode(blurHash, width, height, punch);
        const canvas = createCanvas(width, height);
        const ctx = canvas.getContext('2d');
        const imageData = ctx.createImageData(width, height);
        imageData.data.set(pixels);
        ctx.putImageData(imageData, 0, 0);
        setBlurDateUrl(canvas.toDataURL());
    }, [blurHash, height, punch, width]);

    return [blurDataUrl];
}

from use-next-blurhash.

ivansevillaa avatar ivansevillaa commented on August 16, 2024

Hi @kikoanis, thanks for the comment ๐Ÿ˜ƒ Just a question, why do you need to change the blurDataUrl

from use-next-blurhash.

kikoanis avatar kikoanis commented on August 16, 2024

@ivansevillaa The blurDataUrl might take some time to be generated (however it is small) which might lead for it to be empty at the time of the first rendering. This is common with all async processes.

from use-next-blurhash.

ivansevillaa avatar ivansevillaa commented on August 16, 2024

@kikoanis Are you ok with flickering? If you are ok, it is good for me. Could you create a PR?.
However, you will have this problem yet

I implemented it the same way as stated in the readme but I am getting the error that blurDataURL isn't set.
I assume that this is because useEffect is used so the blurDataURL is empty on render.

I think this best way to use the hook for client-side is just use a fallback url

from use-next-blurhash.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.