E
E
Eugene2021-11-17 16:33:59
React
Eugene, 2021-11-17 16:33:59

How to use different paths to images on different media queries in nextjs?

In normal html5, I can pictureuse multiple sources and specify which images to display at a particular resolution. In next.js, I understand that it does this automatically, but how can I indicate the path to a completely different image using Nekt's one on a certain breakpoint ? Here is the question. Who knows, please tell me :) Tags: next.js, next PS: There was no next.js in the tags, so I wrote it here. <Image/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Ivanovich, 2021-11-18
@toohappy

There is currently no native solution from next for your question. The most banal option is to do it manually:

const getSpecificImage = (breakpoint) => {
  switch (breakpoint) {
    case 1200:
      return <Image src="https://google.com?v=1200" />;

    case 600:
      return <Image src="https://google.com?v=600" />;

     default:
      return <Image src="https://google.com?v=default" />;
  }
};

const breakpoint = window?.innerWidth;

const imageElement = getImageSpecificImage(breakpoint);

return (
  <>
    {imageElement}
  </>
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question