Comments (11)
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.
I had the same problem, I removed the code from the useEffect
and it started working.
from use-next-blurhash.
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.
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.
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.
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.
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.
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.
Hi @kikoanis, thanks for the comment ๐ Just a question, why do you need to change the blurDataUrl
from use-next-blurhash.
@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.
@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 becauseuseEffect
is used so theblurDataURL
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)
- SSR? HOT 2
- suggestion: don't use canvas anymore HOT 3
- Installable package broken due to questionable code change in build HOT 1
- hook returns gray image HOT 10
- Not working on latest version of nextjs HOT 1
- hydration error when using SSR HOT 5
- How to not block rendering? HOT 1
- [Solved] Vercel Deployment Error (version `ZLIB_1.2.9' not found) HOT 17
- Why a hook? HOT 2
- Error: Cannot find module '../build/Release/canvas.node' HOT 3
- Install Error npm HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from use-next-blurhash.