Search code examples
reactjshookrouter

How to pass props through useRoutes in react hook router?


I'm new to react and react hooks. I'm using react-hooks for one of my project. I'm using hookrouter package and I tried to googling about the question, but didn't find much help.

What I want?

I'm trying to pass props through hook routes in react. e.g. , I want to pass object through useRoutes(routes).

Any help will be appreciated. Thanks in advance!


Solution

  • Actually, I was confused and was thinking of passing props through useRoutes instead of Components individually inside routes. Doc

    const routes = {
        '/': () => <HomePage />,
        '/about': () => <AboutPage props={yourProps} />,
        '/products': () => <ProductOverview props={yourProps} />,
        '/products/:id': ({id}) => <ProductDetails id={id} />
    };
    
    const MyApp = () => {
        const routeResult = useRoutes(routes);
    
        return routeResult || <NotFoundPage />;
    }