React Router V4 - uncatch TypeError: location.search.charAt



因为 React Router 从"query"变成了"search"。 我使用以下代码从搜索中查询

查询功能:

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

如何解决此问题?

您必须传递一个字符串来搜索属性,如文档中所述,而不是对象

对我有用的其他事情是这样的:

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

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

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

最新更新