Skip to main content

Get the current route in React

To get the current route, use the useLocation() hook.

if still getting Error: useRoutes() may be used only in the context of a component.

use  <BrowserRouter>  in index.php file rather than app.js

You should have a <BrowserRouter>  higher up in the tree. Note that higher up means that it can't be in the <App> itself, but at least in the component that renders it. 
As we know that index.js render app component so use <BrowserRouter> in index.php

import ReactDOM from 'react-dom'; 
import { BrowserRouter } from 'react-router-dom';


ReactDOM.render( 
 <BrowserRouter> 
   <App /> 
 </BrowserRouter>, 
 document.getElementById('root'), 
); 
 

Add new comment

Restricted HTML

  • You can use shortcode for block builder module. You can visit admin/structure/gavias_blockbuilder and get shortcode, sample [gbb name="page_home_1"].
  • You can use shortcode for block builder module. You can visit admin/structure/gavias_blockbuilder and get shortcode, sample [gbb name="page_home_1"].