Skip to content
Advertisement

How to display image from src/images without using import at the top and without using public directory in react/reactjs

I want to render the star1.png located in src/images/ directory without using import at the top and without using the public directory.

My code:

import React from "react"
import ReactDOM from "react-dom"

class App extends React.Component
{
    render(){
        let starIcon = "./images/star1.png"

        return(
            <>
                <h1>Hello</h1>
                <img src={starIcon} />
            </>
        )
    }
}

ReactDOM.render(<App />, document.getElementById("root"))

Advertisement

Answer

I found the answer to my question which is using require:

let starIcon = require("./images/star1.png")
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement