upgraded packages in package.json file and created useWithRouter hook
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import React from 'react'
|
||||
import { BrowserRouter, Redirect, Route, Switch } from 'react-router-dom'
|
||||
import { BrowserRouter, Redirect, Route, Routes } from 'react-router-dom'
|
||||
import Footer from './components/Footer'
|
||||
import Navbar from './components/Navbar'
|
||||
import Home from './pages/Home'
|
||||
@@ -9,12 +9,12 @@ import Search from './pages/Search'
|
||||
const AppRouter = () => (
|
||||
<BrowserRouter>
|
||||
<Navbar />
|
||||
<Switch>
|
||||
<Route path='/' exact render={() => <Redirect to='/browse' />} />
|
||||
<Routes>
|
||||
<Route path='/' exact render={() => <Navigate to='/browse' />} />
|
||||
<Route path='/browse' component={Home} />
|
||||
<Route path='/search' component={Search} />
|
||||
<Route component={NotFound} />
|
||||
</Switch>
|
||||
</Routes>
|
||||
<Footer />
|
||||
</BrowserRouter>
|
||||
)
|
||||
|
||||
17
src/hooks/useWIthRouter.js
Normal file
17
src/hooks/useWIthRouter.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { useLocation, useNavigate, useParams } from 'react-router-dom'
|
||||
|
||||
/**
|
||||
* @description Provides react router dom props
|
||||
* @param {*} Component React component - JSX Element
|
||||
* @returns React component with route props
|
||||
*/
|
||||
export const withRouter = (Component) => {
|
||||
const ComponentWithRouterProp = (props) => {
|
||||
let location = useLocation()
|
||||
let navigate = useNavigate()
|
||||
let params = useParams()
|
||||
return <Component {...props} router={{ location, navigate, params }} />
|
||||
}
|
||||
|
||||
return ComponentWithRouterProp
|
||||
}
|
||||
Reference in New Issue
Block a user