【发布时间】:2022-01-10 21:22:45
【问题描述】:
我在我的 URL 中显示两个查询字符串,例如 **localhost3000/?filter=all&search=something**
但问题是当没有任何搜索内容时,我的 URL 仍然显示 **localhost3000/?filter=all&search=**
虽然我想显示搜索是否使用并且没有搜索时显示**localhost3000/?filter=all**
这是我的代码:
replaceUrl = () => {
const x = {
filter: this.state.filter,
search: this.state.searchterm,
};
const y = queryString.stringify(x);
this.props.navigate(`?${y}`);
};
replaceUrl 在过滤器更改或搜索内容时调用
【问题讨论】:
-
searchterm未设置时的值是多少?也许是空字符串而不是undefined?请尝试将其设置为undefined。
标签: javascript reactjs react-router query-string react-class-based-component