Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Since React Router change from "query" to "search". I use the following code to query from search

Query function:

  setFilter(query) {
        this.props.history.push({ pathname: this.props.location.pathname, search: query });

Search Parameter:

 applyFilter() {
    const newFilter = {};
    if (this.state.content) {
        newFilter.content = this.state.content;
    if (this.state.fromDate) {
        newFilter.fromDate = this.state.fromDate;
    if (this.state.toDate) {
        newFilter.toDate = this.state.toDate;
    this.props.setFilter(newFilter);

But I get the following error:

Uncaught TypeError: location.search.charAt is not a function

How can I fix this problem?

@BhojendraRauniyar query is an object that he is passing to search, under applyFilter function he is creating an object and passing that too setFilter function as query – Hemadri Dasari Sep 7, 2018 at 10:36 @Think-Twice, that's correct, I just don't know how to use history.push() since I learn from the react-router v2. – sirisakc Sep 7, 2018 at 10:40 To fix the issue remove search and keep it like this this.props.history.push({ pathname: this.props.location.pathname}); and use query object where it's needed – Hemadri Dasari Sep 7, 2018 at 10:43

Something else that worked for me is this:

  setFilter(query) {
        const searchParamsObject = new URLSearchParams(query);
        this.props.history.push({ pathname: this.props.location.pathname, search: searchParamsObject.toString() });

As mentioned in comments. You need to remove query object to fix the issue

setFilter(query) {
    this.props.history.push({ pathname: this.props.location.pathname});
    //use query object here or wherever required
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.