What's the best way to add dynamic images in page components?
See original GitHub issueI’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:
- Created 6 years ago
- Reactions:11
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
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.