I created a component that renders images
this is the component.
import React, { lazy, Suspense } from "react";
const Icon = (props) => {
const { src } = props;
return (
<img src={src} />
);
};
export default Icon;
then I use it like this
import ExampleIcon from "./../images/icons/example.png";
...
<Icon src={ExampleIcon} />
is there a more efficient way to load the icons? and then just "load" example.png and use it as a source? tried to change it to:
const Icon = (props) => {
const src = lazy(() => import("./../images/icons/" + props.src + ".png"));
return (
<Suspense fallback={<p>loading...</p>}><img src={src} /></Suspense>
);
};
looks like it doesn´t work that way. any other ideas? thanks!
loading="lazy"attribute. I'm not leaving this as an answer as I am unsure as to which direction you're trying to go and haven't actually tested this solution.<img src={src}/>? Because it won't actually run a fetch for the image until theimgis rendered.dynamically load images- is very ambiguous statement. What is it that you want to do? If you want to lazy load the images, loading="lazy" might help you out and if you want to implement some sort of code splitting then Lazy and Suspense are the one to be used. @technicallynick's comment also makes a lot of sense. It will help if you can explain what is the end result you want.