-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Svg images as props #11794
Svg images as props #11794
Comments
Are there any errors? Either in terminal running |
Are there any errors? Either in the terminal running, What is the result of using your second code snippet?
|
@Alfrex92 i've picked up on your issue and it looks like there's a bit of confusion regarding on how Gatsby handles assets. You can avoid this through a couple of ways:
Below will be demonstrated both approaches. This are based on a new Gatsby site, created with the hello world starter. For the
import React from 'react'
import uuid from 'uuid'
import { withPrefix } from 'gatsby';
const Skill = props => {
const { skillNames } = props
return (
<div>
{skillNames.map(item => {
return (
<div key={`svg_image_container${uuid.v4()}`}>
<img
key={`svg_image_${uuid.v4()}`}
src={withPrefix(item.icon)}
alt={item.title}
/>
</div>
)
})}
</div>
)
}
export default Skill As you can see it's almost identical to the one you have, the only exception is
import React from 'react'
import Skill from '../components/Skill'
export default () => {
const svgInfo = [
{
icon: './images/beacon.svg',
title: 'beacon',
},
{
icon: './images/osi.svg',
title: 'osi',
},
{
icon: './images/pencil.svg',
title: 'pencil',
},
{
icon: './images/semweb.svg',
title: 'semweb',
},
{
icon: './images/star.svg',
title: 'star',
},
{
icon: './images/sync.svg',
title: 'sync',
},
{
icon: './images/unicode.svg',
title: 'unicoder',
},
{
icon: './images/vnu.svg',
title: 'vnu',
},
{
icon: './images/vote.svg',
title: 'voting',
},
{
icon: './images/whatwg.svg',
title: 'what',
},
{
icon: './images/why.svg',
title: 'why',
},
{
icon: './images/wireless.svg',
title: 'wireless',
},
]
return <Skill skillNames={svgInfo} />
} As the static folder exists and is populated when each and every item of the array is iterated and rendered, Gatsby will understand the file location and show you the contents, as you can see below: To achieve the same result, but this time with the
module.exports = {
siteMetadata: {
title: `svg images as props`,
description: `This barebones site demonstrates how to add svg images`,
},
plugins: [{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
// this (optional) plugin enables Progressive Web App + Offline functionality
// To learn more, visit: https://gatsby.app/offline
// 'gatsby-plugin-offline',
],
}
import React from 'react'
import uuid from 'uuid'
const SkillGraphql = props => {
const { skillNames } = props
return (
<div>
{skillNames.map(item => {
return (
<div key={`svg_image_container${uuid.v4()}`}>
<img
key={`svg_image_${uuid.v4()}`}
src={item.node.publicURL}
alt={`svg_${item.node.publicURL}`}
/>
</div>
)
})}
</div>
)
}
export default SkillGraphql And finally a new page with the following code: import React from 'react';
import { graphql } from "gatsby"
import SkillGraphql from '../components/Skill-Graphql'
const graphqlSourcePage=({ data })=>{
return (
<SkillGraphql skillNames={data.allFile.edges}/>
)
}
export const query=graphql`
query {
allFile(filter: {extension: {eq: "svg"}}) {
edges {
node {
publicURL
}
}
}
}
`;
export default graphqlSourcePage As you can see the svg are loaded if i head onto Also sorry for the svg shown in the images not being resized, forgot about it. |
@jonniebigodes thank you sooo much for the detailed explanation. I understand it perfectly, I appreciated it =D It is working. I have one more question. One of the features that I like most about Gatsby is that automatically converts my img to data URI. With these two methods that you explained to me my img are not being converted to data URI. Is there any way where I can pass images as props and automatically converted it to data URI? *As feedback, might be a good idea to add your explanation in this page. It was clear and easy to understand, I am sure that someone else will have the same question. |
@Alfrex92 No need to thank, i'm glad i could help you out and shed some insights to answer your issue. To answer your question regarding data uris, out of the box no. you can't. For that to work you would have to open each file, convert it to base64 format, escape it's content and suplly it to the What you might be accustomed to see with gatsby and the images being shown as data uris, is that Regarding your second item, the feedback. If you want to do it, contribute to the documentation with this addition, i'm more than happy that you do it, grab the explanation i've provided, let me know and i'll set up a github repo with the code i've used for your issue and proceed from there. Sounds good? |
I got it, thanks for the explanation :D |
@Alfrex92 Once again, no need to thank, i'm glad i could help. Going to close this issue as it's answered. Once again, if you're willing to make that addition to the documentation let me know. |
Hi, I've got a question how can I send SVG images as props?
If I do this I can display the image without any problem.
But I can't display an image as a prop like this :
Any idea of how can I display images as props?
Thanks
The text was updated successfully, but these errors were encountered: