javascriptreactjsgatsbygatsby-imagereact-dom-server

Injecting gatsby-image into html created from markdown using ReactDOMServer.renderToString


Minimum Reproducible Example on Github


I'm trying to inject some images into my pages created from markdown. I'm trying to do this using ReactDomServer.renderToString()

const componentCreatedFromMarkdown = ({data}) => {
...    
    useEffect(() => {
        const injectDivs = Array.from(document.getElementsByClassName('injectDivs'))
        injectDivs.forEach((aDiv) => {
            aDiv.innerHTML = ReactDOMServer.renderToString(<Img fluid={data.allFile.edges[0].node.childImageSharp.fluid} />)
        }
    })
...
}

The img is showing as a black box, if I right click the image I can open it in a new tab, which shows the image as it is supposed to be.


enter image description here


Solution

  • How I understand the problem

    Useful diagnostic information

    Source of the problem

    Direct fix

    Recommended approach

    Hope that helps! 😄