6

I use higher level filter component to add query parameters to URL so that the users could share links with different types of filter.

I am exporting component withRouter() and everything seems legit - I get history injected into component props. However when I call this piece of code:

this.props.history.push({
        pathname: this.props.history.location.pathname,
        query: { tags: selectedTags }
    });

it does change the state of this.props.history and I can see my query present but the URL in browser does not change. Am I doing something wrong?

2 Answers 2

4

You are using it wrong, your code should be:

this.props.history.push({
        pathname: this.props.history.location.pathname,
        search: `?tags=${ selectedTags }`
    });

You can read more about navigation: https://github.com/ReactTraining/history#navigation

Sign up to request clarification or add additional context in comments.

4 Comments

Ah, thanks. But why does this display the previous state? If I push search: ?tags=${ selectedTags }` it on performing an action it will not show up the first time. If I add another query params say search: ?cars=${ selectedTags }` and push that on action the url will now display only ?tags. Seems like there is one state lag.
What value does selectedTags have?
I figured out the problem: two redux actions were fired at the same time one of which is Location change for react-router so I went and fixed that. This brings me to the next problem which is vaguely discussed on the internet: changing search string without triggering route change. The reason behind this is to build up a sharable link for a sort of data mining WEB APP where making lot of calls is expensive and unresponsive in terms of UX.
You can just when object with only property search or use native HTML5 Push State
0

I have encountered the same situation. My solution is to use pushState() method of browser's History API which can change the URL in browser. In terms of changing page content I use React's setState() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.