0

So I have a component that looks like this

import React from "react";

interface CInterface {
  name: string;
  word: string;
  path: string;
}

export function C({ name, word, path }: CInterface) {
  return (
    <div>
      <h2>{name}</h2>
      <>
        <img src={path} alt={name + "" + word} width="600" height="300" />
      </>
    </div>
  );
}

and the app.tsx file is like this

import React from "react";
import { C } from "./components/c";

function App() {
  return (
    <div className="App">
      <h1> K </h1>
      <ul>
        <li>
          {" "}
          <C name="B1" word=" is the best " path="./Images/B1.png" />
        </li>
      </ul>
    </div>
  );
}

export default App;

But the image doesn't load. How do i make it dynamic?

I want to pass both the image name and image path to the component.

1 Answer 1

4

Try to import your image and then pass it to path:

...
import b1Image from './Images/B1.png';
...
<Cat name="B1" word=" is the best " path={b1Image} />
...

To learn more: https://create-react-app.dev/docs/adding-images-fonts-and-files/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.