• Technologies
  • AI Tips and Tricks
  • Frontend
  • Backend
  • Server
  • Contact
  • About
Wednesday, May 14, 2025
Adnan Halilovic Blog
  • Technologies
    • All
    • Angular
    • Git
    • JavaScript
    • ReactJS
    VS Code Setup for Angular Developers Featured Image

    Best VS Code Setup for Angular Development

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Quokka.js and Wallaby.js Giveaway

    Quokka.js and Wallaby.js Giveaway

    How to Work on Different Git Branches at the Same Time?

    How to Work on Different Git Branches at the Same Time?

    Lazyload Standalone Components in Angular

    Lazyloading Standalone Components in Angular

    Standalone Components Angular

    How to Create Standalone Components in Angular

    Private Routes in React Router V6

    Protecting Routes in React JS

    How to setup routing in ReactJS

    How to setup routing in ReactJS

    Retry Error HTTP Requests in Angular (without retryWhen)

    Retry Error HTTP Requests in Angular (without retryWhen)

  • AI Tips and Tricks
  • Frontend
  • Backend
  • Server
  • Contact
  • About
No Result
View All Result
  • Technologies
    • All
    • Angular
    • Git
    • JavaScript
    • ReactJS
    VS Code Setup for Angular Developers Featured Image

    Best VS Code Setup for Angular Development

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Quokka.js and Wallaby.js Giveaway

    Quokka.js and Wallaby.js Giveaway

    How to Work on Different Git Branches at the Same Time?

    How to Work on Different Git Branches at the Same Time?

    Lazyload Standalone Components in Angular

    Lazyloading Standalone Components in Angular

    Standalone Components Angular

    How to Create Standalone Components in Angular

    Private Routes in React Router V6

    Protecting Routes in React JS

    How to setup routing in ReactJS

    How to setup routing in ReactJS

    Retry Error HTTP Requests in Angular (without retryWhen)

    Retry Error HTTP Requests in Angular (without retryWhen)

  • AI Tips and Tricks
  • Frontend
  • Backend
  • Server
  • Contact
  • About
No Result
View All Result
Adnan Halilovic Blog
No Result
View All Result

How to setup routing in ReactJS

Adnan Halilovic by Adnan Halilovic
September 7, 2022
in Frontend, ReactJS
0
0
SHARES
121
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Setting up routing in ReactJS is an important step that could be done in a few ways. In this article we are going to see the one I prefer the most, the object-based routing.

Object-based routing gives us the ability to write an object that has a list of the routes and components that will be utilized in the apps that we create. This object is currently going through the process of being “converted” into standard route elements.

Routing Configuration

The first thing, after creating our components, is to create a config file named routes-config.js:

import About from '../../features/about/about';
import Contact from '../../features/contact/contact';
import Home from '../../features/home/home';
import NotFound from '../../features/not-found/not-found';
import appRoutes from './routes';

const routesConfig = [
  {
    path: '/home',
    element: <Home />,
  },
  {
    path: '/about',
    element: <About />,
    children: [
      {
        path: '/about/home',
        element: <Home />,
      },
    ],
  },
  {
    path: '/contact',
    element: <Contact />,
  },
  {
    path: '*',
    element: <NotFound />,
  },
];

export default routesConfig;

As you could see in the code, we have created the config array of objects. Every single object in the array can have multiple keys, such as:

  • path (path to our route)
  • element (a component that is going to be used on that route)
  • children (an array of route objects for nesting routes inside our components)

Route Renderer

Here is a tiny component for rendering the routes. We need this component because we have to call it from our router. You will see the implementation later on.

import { useRoutes } from 'react-router';
import routesConfig from './routes-config';

const RouteRenderer = () => {
  const routes = useRoutes(routesConfig);
  return routes;
};

export default RouteRenderer;

You may have seen that we have included useRoutes, a hook from react-router, in this particular file. This feature was added in the react-router version 6 release. So, if you are planning to use this hook, you will have to use react-router v6 or above.

The hook (useRoutes) is converting our list of objects into router components that will be called in the following section.

ADVERTISEMENT

Routes in App.js(x)

In our App.js or App.jsx file, we will have to add a router. To do that use the following code:

import { BrowserRouter as Router } from 'react-router-dom';

import RouteRenderer from './core/routes/route-renderer';

import './App.css';

function App() {
  return (
    <Router>
      <RouteRenderer />
    </Router>
  );
}

export default App;

We have imported BrowserRouter as Router from react-router-dom and from now, our router should handle the provided (rendered) routes.

ADVERTISEMENT

Conclusion

This method allows us to easily create a lot of routes, without touching and expanding <Route> elements inside the Router.

More details you can find in the attached video.

If you would like to download a repo with the complete code and setup, you can do it by visiting this Github link.

Source: Adnan Halilovic Youtube Channel
Tags: React RouterReactJSRouting
ADVERTISEMENT
Previous Post

Retry Error HTTP Requests in Angular (without retryWhen)

Next Post

Installing Virtualmin on Ubuntu OS

Related Posts

My Top 5 Uncommon VS Code Extensions for 2024!
Frontend

My Top 5 Uncommon VS Code Extensions for 2024!

January 27, 2024
Mastering DevTools: Supercharge Your Workflow with Code Snippets!
Frontend

Mastering DevTools: Supercharge Your Workflow with Code Snippets!

September 24, 2023
Quokka.js and Wallaby.js Giveaway
Frontend

Quokka.js and Wallaby.js Giveaway

July 26, 2023
Lazyload Standalone Components in Angular
Angular

Lazyloading Standalone Components in Angular

September 11, 2022
Standalone Components Angular
Angular

How to Create Standalone Components in Angular

September 10, 2022
Private Routes in React Router V6
Frontend

Protecting Routes in React JS

September 7, 2022
Next Post
Installing Virtualmin on Ubuntu OS

Installing Virtualmin on Ubuntu OS

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Latest posts

The One MCP Server Every Developer Should Be Using
AI Tips and Tricks

The One MCP Server Every Developer Should Be Using

by Adnan Halilovic
May 13, 2025
0

What Is Context 7 MCP Server? Context 7 is a Model Context Provider (MCP) server that delivers real-time documentation and metadata to...

Read moreDetails
Boosting Productivity with AI-Powered IDEs: My Workflow with Windinsurf

Boosting Productivity with AI-Powered IDEs: My Workflow with Windinsurf

May 12, 2025
Why I Switched from VS Code to Windsurf — An AI Code Editor That Changed My Workflow

Why I Switched from VS Code to Windsurf — An AI Code Editor That Changed My Workflow

May 12, 2025
The Best Coding Font to Boost Your Productivity!

The Best Coding Font to Boost Your Productivity!

October 17, 2024
VS Code Setup for Angular Developers Featured Image

Best VS Code Setup for Angular Development

October 13, 2024
Github LinkedIn Youtube Twitter Pinterest Instagram Reddit

About Me

Adnan Halilovic Blog

Adnan Halilović

Software Developer - Content Writer

I am Adnan Halilović, the man behind the website and channels that you are currently reading and viewing!

I am a software developer with over 15 years of expertise in a variety of fields.

Newsletter

Country:

Email address:


Recent from Instagram

    The Instagram Access Token is expired, Go to the Customizer > JNews : Social, Like & View > Instagram Feed Setting, to refresh it.

© 2022 Adnan Halilovic - Software development content, tips & tricks.

Click to Copy
No Result
View All Result
  • Technologies
  • AI Tips and Tricks
  • Frontend
  • Backend
  • Server
  • Contact
  • About

© 2022 Adnan Halilovic - Software development content, tips & tricks.