Fork me on GitHub
#reagent
<
2021-09-11
>
hanyuone12:09:25

Hi, how would you reference local images (png) in Reagent? What's the equivalent of import RandomImage from "../images/random_image.png"; ?

hanyuone12:09:09

I tried using the (rc/inline "../images/random_images.png") from https://clojureverse.org/t/using-none-code-resources-in-cljs-builds/3745 referenced in a previous post about SVGs but that doesn't seem to work

p-himik12:09:41

What do you expect RandomImage to contain? A data URL? A regular URL where the PNG is copied into the output directory? Something else?

hanyuone01:09:07

I'm pretty sure it'd be a string? Not too certain about React default behaviour when it comes to importing images

hanyuone01:09:30

From https://create-react-app.dev/docs/adding-images-fonts-and-files/: > You can `import` a file right in a JavaScript module. This tells webpack to include that file in the bundle. Unlike CSS imports, importing a file gives you a string value. This value is the final path you can reference in your code, e.g. as the `src` attribute of an image or the `href` of a link to a PDF. > To reduce the number of requests to the server, importing images that are less than 10,000 bytes returns a https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs instead of a path.

hanyuone01:09:05

For now I just stored the images in public/images and referenced them like [:img {:src "images/random_image.png"}] which seems clean enough, but wondering if there's a more idiomatic way to do it similar to how React does it

p-himik03:09:42

That import has nothing to do with React itself - it's the compiler/bundler included with that template that does it. Your solution is exactly how I'd do it - obvious, simple, flexible.