2

I have the following react reusable component, can i use it as react component in the react-router-dom here? I m new to react, thank you so much for your help.......................................................................................................

when i use this code i get error: var Component: ((props: Props) => JSX.Element) | JSX.Element JSX element type 'Component' does not have any construct or call signatures.

 const ImageFirst = ({ imgSrc, title, desc, link, linkTitle }: Props) => {
      return (
        <div className="SRFoods">
          <div className="SRFoodsImg">
            <img src={imgSrc} alt="" width="100%" height="auto" />
          </div>
          <div className="content">
            <h1>{title}</h1>
            <p>{desc}</p>
            <h4>
              For More Info, Go to: &nbsp;
              <a href={link} target="_blank" rel="noopener noreferrer">
                {linkTitle}
              </a>
            </h4>
          </div>
        </div>
      );
    };

//App.js page

import ImageFirst from "./components/Pages/ImageFirst";
import SRFoodsImg from './assets/srproducts.png'
import Home from "./pages/Home";

const App: React.FC = () => {
  const routes = [
    { path: "/", name: "Home", Component: Home },
    { path: "/food/sragro", Component: <ImageFirst
    imgSrc={SRFoodsImg}
    title="SR Agro"
    desc="testofdesc"
    linkTitle="http://srfoods.com.np"
    link="http://srfoods.com.np"
    /> },
  ];
  return (
    <BrowserRouter>
      <>
        <Navbar />
        {routes.map(({ path, Component }) => (
          <Route key={path} exact path={path}>
            {({ match }) => (
              <div className="page">
                  <Component />
                </div>
            )}
          </Route>
        ))}
        <Footer />
      </>
    </BrowserRouter>
    );
  };
1
  • 1
    Seems like a really simple thing to test. Have you tried it? I suspect it won't work and you'll need to render an anonymous component, i.e. component: () => <ImageFirst ......./>. Commented Feb 25, 2021 at 8:01

1 Answer 1

1

I am fairly certain you can't pass a fully instantiated component to the component prop of a Route, but you can pass an anonymous functional component.

const routes = [
  {
    path: "/",
    name: "Home",
    Component: Home,
  },
  {
    path: "/food/sragro",
    Component: () => (
      <ImageFirst
        imgSrc={SRFoodsImg}
        title="SR Agro"
        desc="testofdesc"
        linkTitle="http://srfoods.com.np"
        link="http://srfoods.com.np"
      />
    ),
  },
];

This would be nearly the equivalent of defining a component ahead of time.

const CustomImageFirst = () => (
  <ImageFirst
    imgSrc={SRFoodsImg}
    title="SR Agro"
    desc="testofdesc"
    linkTitle="http://srfoods.com.np"
    link="http://srfoods.com.np"
  />
);

...

const routes = [
  {
    path: "/",
    name: "Home",
    Component: Home,
  },
  {
    path: "/food/sragro",
    Component: CustomImageFirst,
  },
];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.