React Router v4 - Redirect to same route with different query params

prolific

Scenario:

There is a homepage from where on searching user is redirected to a search page with a query param (eg q=abc). Now being on search page, user again tries to search for a different value so he is again redirected to same search page but with a different query param (eg q=xyz)

Problem:

When I am trying to perform a Redirect from Search page again to the same route with a different query param, I am getting an error "You tried to redirect to the same route you're currently on".

Expected Result:

User should be redirected to the same route again with a different query params so that on pressing back button he can go back to the previous query like it would normally be if it hadn't been a SPA. The result should be similar to the case where I had redirected the user from /search/abc to /search/xyz both using the same component.

Possible Solution:

On submitting a search query, I can check if it is the same route and based on that I can either redirect or update the component state some way. Problem with this solution is that user cannot go back to the previous query on clicking browser's back button. I want the page to be added in history.

Code Snippet:

Using below code for redirection in render function inside Search Component which is present on both homepage and search page

if (this.state.isReady) {
        let queryString = "q=" + this.state.q;
        return (
            <Redirect push to={`/search/hotels?${queryString}`} />
        );
    }

I think this is a common problem with any site having a search feature so there should be an elegant way to handle such a situation but I am unable to think about it.

prolific

Found A Solution:

Instead of using <Redirect /> from React Router, I used history api to push a new route.

this.props.history.push(`/search/hotels?${queryString}`);

I wrapped my SearchComponent inside withRouter which is a HOC provided by React-Router and then used the history api.

Code Snippet:

import { withRouter } from 'react-router-dom';

class SearchComponent {

    // Component Code goes here

    onSubmit(){
        let queryString = "q=" + this.state.q;
        this.props.history.push(`/search/hotels?${queryString}`);
    }
}

export default withRouter(SearchComponent)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

React Hooks with React Router v4 - how do I redirect to another route?

分類Dev

React Router redirect using alternate route configuration

分類Dev

React Router Default Route Redirect to /home

分類Dev

React Router v4 Redirectユニットテスト

分類Dev

Prevent rendering of a particular layout when on a specific route with react router dom v4?

分類Dev

PropTypesisRequired on React Router 4 params prop

分類Dev

React router v4 Link to

分類Dev

React Router Match with Params

分類Dev

Different component in a same React route based on roles

分類Dev

route router v4 does not reflect component change with Link?

分類Dev

react-navigation: Navigate to same RouteName but with different params

分類Dev

React router is not reloading page when url matches the same route

分類Dev

histroy.push() is not working in react router, if my base route is same

分類Dev

react-router: cant display nested route in different view

分類Dev

Angular 6 router loses route params

分類Dev

Update route not working on merged express router params

分類Dev

RedirectまたはLinkを使用せずにreact-router v4でURLを変更する

分類Dev

React router redirect

分類Dev

react-router(v4)戻る方法は?

分類Dev

Navigating Programmatically in React-Router v4

分類Dev

Navigating Programmatically in React-Router v4

分類Dev

Navigating Programmatically in React-Router v4

分類Dev

React Router v4 with babel gives error with multiple routes

分類Dev

Passing props to children components with react-router v4

分類Dev

React-Router v4 onEnter replacement

分類Dev

React-Router v4 + Redux-Saga navigation

分類Dev

Same ip, different route?

分類Dev

Htaccess 301 redirect with query string params

分類Dev

'react-router'を 'react-router-dom'に移行します(v4)

Related 関連記事

  1. 1

    React Hooks with React Router v4 - how do I redirect to another route?

  2. 2

    React Router redirect using alternate route configuration

  3. 3

    React Router Default Route Redirect to /home

  4. 4

    React Router v4 Redirectユニットテスト

  5. 5

    Prevent rendering of a particular layout when on a specific route with react router dom v4?

  6. 6

    PropTypesisRequired on React Router 4 params prop

  7. 7

    React router v4 Link to

  8. 8

    React Router Match with Params

  9. 9

    Different component in a same React route based on roles

  10. 10

    route router v4 does not reflect component change with Link?

  11. 11

    react-navigation: Navigate to same RouteName but with different params

  12. 12

    React router is not reloading page when url matches the same route

  13. 13

    histroy.push() is not working in react router, if my base route is same

  14. 14

    react-router: cant display nested route in different view

  15. 15

    Angular 6 router loses route params

  16. 16

    Update route not working on merged express router params

  17. 17

    RedirectまたはLinkを使用せずにreact-router v4でURLを変更する

  18. 18

    React router redirect

  19. 19

    react-router(v4)戻る方法は?

  20. 20

    Navigating Programmatically in React-Router v4

  21. 21

    Navigating Programmatically in React-Router v4

  22. 22

    Navigating Programmatically in React-Router v4

  23. 23

    React Router v4 with babel gives error with multiple routes

  24. 24

    Passing props to children components with react-router v4

  25. 25

    React-Router v4 onEnter replacement

  26. 26

    React-Router v4 + Redux-Saga navigation

  27. 27

    Same ip, different route?

  28. 28

    Htaccess 301 redirect with query string params

  29. 29

    'react-router'を 'react-router-dom'に移行します(v4)

ホットタグ

アーカイブ