1

I want to add params to the current URL, for example if I do a query or I am looking from records 10 to 20 I just want the URL to reflect on that without reloading. For example: http://myurl/?q=searchworkd&min=10&results=10.

I having being searching for this but I am confused. All the examples usually talk about redirecting or adding new params and I dont if I should use history, browserHistory or hashHistory. All the

1
  • I'd recommend import {useHistory} from 'react-router-dom' and do something like history.push('/yoururl?results=20') Commented Nov 7, 2019 at 18:41

2 Answers 2

3

You can import {useHistory} from 'react-router-dom and do this:

const history = useHistory();

// console.log(history);

const handleButtonClick = (e) => {
    let newResults = 20;
    history.push(`/?q=searchworkd&min=10&results=${newResults}`);
}
Sign up to request clarification or add additional context in comments.

1 Comment

how would I accomplish the same using react-router-dom-v6?
1

In a new version useHistory() does not work. Instead, use useNavigate function to accomplish desired result.

import {useNavigate} from "react-router-dom";

const navigate = useNavigate();


const handleButtonClick = (e) => {
let newResults = 20;
navigate(`/?q=searchworkd&min=10&results=${newResults}`);
}

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.