So far our Pokedex app only has a single page.
Let's make this more interesting…
Let's make this more interesting…
We want to define distinct pages within our application.
The current page at a given time is defined by the URL of the browser tab.
The URL can contain parameters which are used by a page to show specific data.
React does not provide routing.
The most popular routing library in the React ecosystem is React Router.
The BrowserRouter provides the access to the routing functionality.
A Routes component renders the most specific matching route (exclusively).
Here we define a route with the path /page1 .
The page to be renderered for this route is simply defined as element of Route .
Page1.tsx
The "page" is just a normal React component.
This route has a parameter carId .
CarDetailPage.tsx
The useParams Hook returns the parameters of the current route as an object.
With the Navigate component, a route can be redirected to another route.
<BrowserRouter />
<Routes />
<Route path="/cars/:carId" element={<CarDetailPage />} />
<Navigate to="/cars" replace />
<Link to="/cars/42">Go to Car 42</Link>
GET https://pokeapi.co/api/v2/pokemon/ name
{
"name": "bulbasaur",
sprites: {
"front_shiny": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/1.png",
...
},
...
}
Think about what to do with the username input and display functionality.
Can we have the input on a Profile page but show the name on all pages?
App.tsx
PokeListEntry.tsx
DetailPage.tsx
A NavLink is marked with the specified CSS class/style when its route is active.
Current URL: /cars?filter=Tesla
Returns the location object representing the current URL.
Navigate programmatically
Use instead of BrowserRouter for tests.
We learned…