Skip to content

Added Child Routes From App and Home #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import NavBar from "./components/NavBar"
import { Outlet } from "react-router-dom"

function App() {
return (
<>
<header>

<NavBar />
</header>
<Outlet />
</>
);
};
)
}

export default App;
export default App
46 changes: 21 additions & 25 deletions src/pages/Home.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import { useState, useEffect } from "react";
import UserCard from "../components/UserCard";
import NavBar from "../components/NavBar";
import { useState, useEffect } from "react"
import UserCard from "../components/UserCard"
import { Outlet } from "react-router-dom"

function Home() {
const [users, setUsers] = useState([]);
const [users, setUsers] = useState([])

useEffect(() =>{
useEffect(() => {
fetch("http://localhost:4000/users")
.then(r => r.json())
.then(data => setUsers(data))
.catch(error => console.error(error));
}, []);
const userList = users.map(user =>{
return <UserCard key={user.id} user={user}/>;
});
.then((r) => r.json())
.then((data) => setUsers(data))
.catch((error) => console.error(error))
}, [])

const userList = users.map((user) => {
return <UserCard key={user.id} user={user} />
})

return (
<>
<header>
<NavBar />
</header>
<main>
<h1>Home!</h1>
{userList}
</main>
</>
);
};
<main>
<h1>Home!</h1>
<Outlet />
{userList}
</main>
)
}

export default Home;
export default Home
54 changes: 30 additions & 24 deletions src/routes.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import Home from "./pages/Home";
import About from "./pages/About";
import Login from "./pages/Login";
import UserProfile from "./pages/UserProfile";
import ErrorPage from "./pages/ErrorPage";
import App from "./App"
import Home from "./pages/Home"
import About from "./pages/About"
import Login from "./pages/Login"
import UserProfile from "./pages/UserProfile"
import ErrorPage from "./pages/ErrorPage"

const routes = [
{
path: "/",
element: <Home />,
errorElement: <ErrorPage />
},
{
path: "/about",
element: <About />,
errorElement: <ErrorPage />
},
{
path: "/login",
element: <Login />,
errorElement: <ErrorPage />
element: <App />,
errorElement: <ErrorPage />,
children: [
{
path: "/",
element: <Home />,
children: [
{
path: "/profile/:id",
element: <UserProfile />,
},
],
},
{
path: "/about",
element: <About />,
},
{
path: "/login",
element: <Login />,
},
],
},
{
path: "/profile/:id",
element: <UserProfile />,
errorElement: <ErrorPage />
}
];
]

export default routes;
export default routes