Dejan posted: " The Image component is very much a key selling point for the framework. This is a must use and situation inevitably arises to use it in a custom component that we'd like to test out in Storybook. This issue has come back to haunt again with t"
The Image component is very much a key selling point for the framework. This is a must use and situation inevitably arises to use it in a custom component that we'd like to test out in Storybook.
This issue has come back to haunt again with the release of Next.
To simply remove this error in storybook, we just need to apply the `unoptimized` prop to the `next/image` component and voila! Storybook is back and working again.
But this isn't how we want roll in production so we need to find a way to apply this prop to be false only in production code. A strategy I found useful here to use is the React Context. So we begin by creating the Provider and Context which has the unoptimized prop and set to false by default.
import React from 'react'; interface ImageOptions { unoptimized: boolean; } interface ProviderProps extends ImageOptions { children: React.ReactNode; } export const ImageOptimisationContext = React.createContext<ImageOptions>({ unoptimized: false }); // This provider is useful for allowing storybook to use a unoptimized: true export const ImageOptimisationProvider = ({ children, unoptimized }: ProviderProps) => { return <ImageOptimisationContext.Provider value={{ unoptimized }}>{children}</ImageOptimisationContext.Provider>; };
The new custom wrapper Image component gets access to the React context and applies this property along with the rest of the next/image component. And is very similar to the solution applied previously.
No comments:
Post a Comment