search

Home  >  Q&A  >  body text

Solution to the problem that the Reactjs routing page is displayed as blank

I'm using Reactjs and I'm trying to use "Dynamic Routing" now, but the page appears blank. This is my routing file:

export default function Router() {
  return useRoutes([
     {       
     path: "/",
     element: <Layout />,
     errorElement: <Page404 />,
     children: [
        { element: <HomePage />, index: true },
        { element: <User />, index: true },
                ],     
        },  
        ]); 
}

const HomePage = Loadable(lazy(() => import("../pages/HomePage")));
const User = Loadable(lazy(() => import("../pages/User")));
const Page404 = Loadable(lazy(() => import("../pages/Page404")));

I'm trying to access the "User.js" file in (src/pages), this is my User.js file:

import React, { useEffect, useState } from "react";
import { useParams, withRouter } from "react-router";
import axios from "axios";
const User = (props) => {
    const params = useParams();
    const [users, setUsers] = useState({});
    useEffect(() => {
        async function fetchData() {
            const res = await axios(
                `https://jsonplaceholder.typicode.com/comments/${params.id}`
            );
            console.log("INDI", res.data);
            setUsers(res.data);
        }
        fetchData();
    }, []);
    return (
        <>
        <div>Hello worldddddddd</div>
        </>
    );
};

export default User;
P粉434996845P粉434996845488 days ago581

reply all(1)I'll reply

  • P粉885035114

    P粉8850351142023-09-07 11:02:47

    The problem is that you added index=true to both child elements. You can only have one index page

    Example:

    return useRoutes([
       {       
       path: "/",
       element: <Layout />,
       errorElement: <Page404 />,
       children: [
          { element: <HomePage />, index: true },
          { element: <User />},
                  ],     
          },  
          ]); 
    }
    
    const HomePage = Loadable(lazy(() => import("../pages/HomePage")));
    const User = Loadable(lazy(() => import("../pages/User")));
    const Page404 = Loadable(lazy(() => import("../pages/Page404")));

    You can learn more about the purpose of index=true from the accepted answer to this question.

    reply
    0
  • Cancelreply