react-router 4不会使用查询参数更新url

Xep*_*ris 6 reactjs react-router-v4

我使用更高级别的过滤器组件将查询参数添加到URL,以便用户可以与不同类型的过滤器共享链接.

我正在导出组件withRouter(),一切似乎都是合法的 - 我将历史注入到组件道具中.但是,当我调用这段代码时:

this.props.history.push({
        pathname: this.props.history.location.pathname,
        query: { tags: selectedTags }
    });
Run Code Online (Sandbox Code Playgroud)

它确实改变了this.props.history的状态,我可以看到我的查询存在,但浏览器中的URL不会改变.难道我做错了什么?

Anh*_*yen 4

你用错了,你的代码应该是:

this.props.history.push({
        pathname: this.props.history.location.pathname,
        search: `?tags=${ selectedTags }`
    });
Run Code Online (Sandbox Code Playgroud)

您可以阅读有关导航的更多信息:https ://github.com/ReactTraining/history#navigation