Dynamic image rendering with react router

Hello everyone, I found a really cool web page on code sandbox and was hoping for a little help understanding it.
Full Code: https://codesandbox.io/s/framer-motion-x-react-router-n7qhp?file=/src/index.js
So from what I understand the code here dynamically renders a router-page based off of the image id.

<Link to={`/image/${i}`}>
            alt="The Barbican"

And then makes the page with a single image

<motion.div className="single" initial="exit" animate="enter" exit="exit">
        images[parseInt(match.params.id, 10)]
      alt="The Barbican"
    <motion.div className="back" variants={backVariants}> Well lets see
      <Link to="/">← Back</Link>

Using a javascript file with the image src’s in it.

export const images = [

I really like this code, however when I tinker with it I can never seem to get the images to work. I would like to replace the images without breaking the whole thing, does anyone have an idea how I would be able to do this?
Thank you for your expertise.

Can you show an example of what you have tried?

If you use an URL that has an id part you just replace the id with that. Otherwise, if you use full URL strings you willl just remove the template string and use id and images[parseInt(match.params.id, 10)] for the src.

Here is an example using unsplash, I didn’t bother messing with the image dimensions so they are not equal height.

Using ids

Using full URLs

1 Like

This is exactly what I needed. Thanks for doing this. I will reverse engineer your methods and better understand them.

I do have one follow up question if you don’t mind. I am trying to use images on my machine, it works great for the thumbnails but the expanded images are only giving the src as 1, 2, 3, or 4.
I know these are the Ids of the photos which is helpful, but is there a way I can get the url to show where the id is showing?

When you say the src, are you saying the single images do not load because you are not getting the correct image path, or are you just talking about the URL shown in the SingleImage component when clicking the images?

The numbers in the URL for the single image is the index of the images array passed to /:id in the router.

1 Like

I actually figured out my error earlier, I appreciate your help!