javascript - React router 4 附加额外的查询参数

标签 javascript reactjs react-router react-router-v4

我的问题类似于 React router appending query to URL但我正在使用带有 Switch 和路由的 React router 4。就我而言,如何排除从历史记录中附加的额外查询参数?

  render(){
return(
  <div>
    <Header/>
    <Switch>
      <Route exact path="/" render={(props) => (<Home passDealsToRoute ={this.dealsCallBack} {...props}/>)}/>
      <Route exact path="/mall/:id/:store_id/:deal_id" render={(props) => (<MallDetail deals ={this.deals} {...props}/>)}/>
      <Route exact path="/mall/:id/:store_id" render={(props) => (<MallDetail deals ={this.deals} {...props}/>)}/>
      <Route exact path="/mall/:id" render={(props) => (<MallDetail deals ={this.deals} {...props}/>)}/>
      <Route path="/home/:location" render={(props) => (<Home passDealsToRoute ={this.dealsCallBack} {...props}/>)} />
      <Route exact path="/about" component={About}/>
    </Switch>
    <Footer/>
  </div>
);}} export default withRouter(Root);

我的索引文件

import React from 'react';


import {BrowserRouter,Route} from 'react-router-dom';
import {render} from 'react-dom';
import Root from './components/root/root';
import { createBrowserHistory } from 'history';
const history = createBrowserHistory({queryKey: false});

class App extends React.Component {
 render() {
   return(
    <BrowserRouter history={history}>
      <Route path={"/"} component={Root}/>
    </BrowserRouter>
   );
 }
 };

 render(<App/>,window.document.getElementById('app'));

最佳答案

返回到您正在使用 index.js 的文件(我猜 <BrowserHistory /> )并添加以下内容:

Note that you will grab createBrowserHistory from "history" package not "react-router-dom" Also "queryKey: false" is the answer here .

import { BrowserRouter } from 'react-router-dom';
import { createBrowserHistory } from 'history';

const history = createBrowserHistory({queryKey: false});
//.....

<BrowserRouter history={history}>
  // Your routes here ...
</BrowserRouter >

注意:

由于您正在利用<Switch />,因此您可以通过删除精确的并反转所有路线来更好地组织您的路线。组件。

关于javascript - React router 4 附加额外的查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46255410/

相关文章:

c# - 如何避免在 asp.net 中单击按钮事件后页面刷新

javascript - 使用javascript将参数解构为函数调用

javascript - 如何使用 Microphone.getMicrophone() Actionscript 3 切换音频源

reactjs - 如何正确创建和部署React App?

reactjs - react 路由器:MemoryHistory

javascript - Jquery 自动完成建议未显示在文本框下,而是显示在 ASP.NET MVC 中的页面底部

javascript - 如何设置下拉菜单中的默认项?

reactjs - 用构造函数替换 componentWillMount() 不起作用

javascript - 将 Webpack 与 React-router bundle.js 一起使用未找到

reactjs - 如何使用 React Router 维护路由之间的状态?