Routing in React
Open the React Router tutorial: React Router Tutorial for reference.
- Create a new project with Vite:
npx create-vite@latestand clean up in the usual way before you continue. - Install
npm i react-routersee documentation here: version 7
We are going to create a book viewing app.
Create a navigation bar with links to:
- Books (showing a list of books)
- Add a book (With a form to add a new book)
- Find a book (With an input field to write an id number to show a book)
Add appropriate routes with components for each link.
Use the
BookFacadefrom here for methods to use on the book list.Implement the
Bookscomponent to show a list of all the books.- Make each book a clickable link.
Add a
Detailscomponent that can show details about a book when the book is clicked on in the list of books (this one should get the id of the book, that was clicked like this:books/:id).Implement the
AddBookcomponent with a form that can add another book to the list.Implement the
FindBookcomponent with a single input field to enter a book id and a button to load book data below the input field.Add a
NoMatchcomponent and add a route to let it handle “unknown” routes.Change the book links to be
NavLinksas in here.