question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What's the best way to add dynamic images in page components?

See original GitHub issue

I’ve been building a site that primarily uses Page components and was wondering about guidance on best practices for images when you have:

  • Pages based on page components
  • Want to have optimized images on the page that I can resize/format in code

I’d be happy to add these to documentation if they seem right. And if not I’d love to learn the best way to do this. Basically I’ve been using

  • gatsby-image
  • gatsby-transformer-sharp
  • gatsby-plugin-sharp
  • gatsby-source-filesystem

I then query the file I want to use as my image and format it (with resolutions for fixed size, size with images that should stretch)

export const query = graphql`
  query indexQuery {
    fileName:file(relativePath: { eq: "images/myimage.jpg" }) {
      childImageSharp {
        resolutions(width: 400) {
          ...GatsbyImageSharpResolutions
        }
      }
    }
  }
`;

I then can use the “fragment” ( like “…GatsbyImageSharpResolutions”) in gatsby-image

import Img from 'gatsby-image'
const imagePage = ({ data }) => {
  <Img resolutions={data.fileName.childImageSharp.resolutions} />
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

18reactions
melissamcewencommented, Apr 10, 2018

@nimaiwalsh I have a PR in for some docs on that https://github.com/gatsbyjs/gatsby/pull/4191 on my own site I’ve been using “image fragments” which are like standard queries that format the images for my gallery on my portfolio. Here’s my fragments https://github.com/melissamcewen/melissamcewen/blob/master/src/utils/imagefragments.js and here is an example: https://github.com/melissamcewen/melissamcewen/blob/master/src/pages/portfolio/index.js

6reactions
nimaiwalshcommented, Apr 10, 2018

What is the best method to query and map multiple images say for a Gallery/Grid of images?

Great project by the way! Great work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Part 7: Add Dynamic Images from Data - Gatsby
This time, you'll learn how to add dynamic images to your site. In this part of the Tutorial, you'll use the dynamic GatsbyImage...
Read more >
Add Dynamic Media Assets to pages
The Dynamic Media component lets you add dynamic images, including image sets, spin sets, and mixed media sets. You can zoom in, zoom...
Read more >
How can I dynamically add images to my pages in Gatsby ...
You can find much better ways than querying each image in a staticQuery filtered by the path, it's not true that is the...
Read more >
How to create an image element dynamically using JavaScript
Create an empty image instance using new Image(). Then set its attributes like (src, height, width, alt, title, etc). Finally, insert it into ......
Read more >
Building an effective Image Component - Chrome Developers
An image component encapsulates performance best practices and provides an out-of-the-box solution to optimize images. Published on Monday, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found