Create React App does not include page routing by default; React Router is the most popular solution for this purpose.
To add React Router to your application, execute the following command in the terminal from the root directory:
npm i -D react-router-dom |
Note: This tutorial uses React Router v6. If you’re upgrading from v5, you’ll need to use the @latest flag. |
npm i -D react-router-dom@latest |
To set up an application with multiple page routes, start by organizing the file structure. Inside the src folder, create a pages directory with the following files:
Each file will contain a basic React component.