0.0.1 • Published 5 years ago
react-real-location v0.0.1
react-real-location
About
Get current location from react-router outside the Switch component
How to Install
First, install the library in your project by npm:
$ npm install react-real-location
Or Yarn:
$ yarn add react-real-location
Getting Started
• Import hook in React application file:
import { useRealLocation } from 'react-real-location';
Returned Values
Type | Description |
---|---|
Location | Current location params |
Example
// App.js
import React from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';
import { useRealLocation } from 'react-real-location';
import Home from './pages/Home';
import Product from './pages/Product';
import Client from './pages/Client';
const App = () => {
const { pathname } = useRealLocation();
return (
<>
<p>{pathname}</p>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/product/:id" component={Product} />
<Route path="/client/:id" component={Client} />
<Redirect from="*" to="/" />
</Switch>
</>
);
};
export default App;
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './App';
ReactDOM.render(
<Router>
<App />
</Router>,
document.getElementById('root')
);
License
This project is licensed under the MIT License © 2020-present Jakub Biesiada