Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing basename of BrowseRouter

I'm setting a basename path like this:

<BrowserRouter basename="/calendar"/>

Is it possible to access that basename property in any of the app's components via React Router somehow? Otherwise I would need to parse the Location object.

I've looked at the history object, but couldn't be found there.

like image 313
Fellow Stranger Avatar asked May 21 '18 12:05

Fellow Stranger


People also ask

What is BrowserRouter basename?

basename: stringThe base URL for all locations. If your app is served from a sub-directory on your server, you'll want to set this to the sub-directory. A properly formatted basename should have a leading slash, but no trailing slash.

How do I get the base URL in react dom router?

Adding a Base URLImport the BrowserRouter component from react-router-dom . The BrowserRouter component has a basename prop, which accepts a string as its value in case the React app is hosted from a sub-directory.

How do you navigate in react dom on a router?

There are two ways to programmatically navigate with React Router - <Navigate /> and navigate() . You can get access to Navigate by importing it from the react-router-dom package and you can get access to navigate by using the custom useNavigate Hook.

What is the difference between BrowserRouter and router?

At the core of every React Router application should be a router component. For web projects, react-router-dom provides <BrowserRouter> and <HashRouter> routers. The main difference between the two is the way they store the URL and communicate with your web server. A <BrowserRouter> uses regular URL paths.


1 Answers

You can use History hook

const history = useHistory();
history.createHref({pathname: '/'})
like image 161
user1034902 Avatar answered Oct 21 '22 09:10

user1034902