please forgive me if there is a duplicate.
I know MemoryRouter has initialEntries and initialIndex, so you can set path and etc for "location" and "history". However "match" is not getting updated... I need to set "match" for my react app and Jest tests.
When I try,
<MemoryRouter initialEntries={['/hello']} initialIndex={0}>
<Hello store={store} />
</MemoryRouter>
I am getting
match: { path: '/', url: '/', params: {} ... },
location: { path: '/hello', pathname: '/', ... },
history: { ..., location: { path: '/hello', pathname: '/', ... }}
I wonder if there is a way to set match. Thanks in advance.
Use the useLocation() hook to get the current route with React Router, e.g. const location = useLocation() . The hook returns the current location object. For example, you can access the pathname as location. pathname .
A <Router> that keeps the history of your “URL” in memory (does not read or write to the address bar). Useful in tests and non-browser environments like React Native.
You can wrap your <Hello store={store} />
component with <Route ... />
component like:
import React from 'react';
import { MemoryRouter, Route } from 'react-router-dom';
// later in tests
<MemoryRouter initialEntries={['/home']}>
<Route component={props => <HomeComponent {...props} />} path="/home" />
</MemoryRouter>
Then you should have access to proper match
object by props
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With