React Router V4 - 未捕获TypeError:location.search.charAt不是函数

问题描述 投票:1回答:2

由于React Router从“查询”更改为“搜索”。我使用以下代码从搜索中查询

查询功能:

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

搜索参数:

 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);
  }

但是我收到以下错误:

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

我该如何解决这个问题?

reactjs react-router
2个回答
1
投票

如评论中所述。您需要删除查询对象以解决问题

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

1
投票

您必须按照docs上的说明将字符串传递给搜索属性,而不是对象

© www.soinside.com 2019 - 2024. All rights reserved.