Skip to content

Commit e0519b5

Browse files
committed
Fix useRedirect does not handle query strings
Closes #6141 Supersedes #6009
1 parent 34dfc0c commit e0519b5

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as React from 'react';
2+
import { useEffect } from 'react';
3+
import expect from 'expect';
4+
import { renderWithRedux } from 'ra-test';
5+
import { createMemoryHistory } from 'history';
6+
import { Router } from 'react-router-dom';
7+
8+
import useRedirect from './useRedirect';
9+
10+
const Redirect = ({ redirectTo, basePath = '', id = null, data = null }) => {
11+
const redirect = useRedirect();
12+
useEffect(() => {
13+
redirect(redirectTo, basePath, id, data);
14+
}, []);
15+
return null;
16+
};
17+
18+
describe('useRedirect', () => {
19+
it('should redirect to the path with query string', () => {
20+
const history = createMemoryHistory();
21+
renderWithRedux(
22+
<Router history={history}>
23+
<Redirect redirectTo="/foo?bar=baz" />
24+
</Router>
25+
);
26+
expect(history.location).toMatchObject({
27+
pathname: '/foo',
28+
search: '?bar=baz',
29+
state: { _scrollToTop: true },
30+
});
31+
});
32+
});

packages/ra-core/src/sideEffect/useRedirect.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { useCallback } from 'react';
22
import { useDispatch } from 'react-redux';
3+
import { useHistory } from 'react-router-dom';
4+
import { parsePath } from 'history';
35

46
import { Identifier, Record } from '../types';
57
import resolveRedirectTo from '../util/resolveRedirectTo';
68
import { refreshView } from '../actions/uiActions';
7-
import { useHistory } from 'react-router-dom';
89

910
type RedirectToFunction = (
1011
basePath?: string,
@@ -53,7 +54,7 @@ const useRedirect = () => {
5354
}
5455

5556
history.push({
56-
pathname: resolveRedirectTo(redirectTo, basePath, id, data),
57+
...parsePath(resolveRedirectTo(redirectTo, basePath, id, data)),
5758
state: { _scrollToTop: true },
5859
});
5960
},

0 commit comments

Comments
 (0)