随机查询字符串出现在react-router中

use*_*870 16 reactjs react-router

看起来很奇怪,当我打开/时,浏览器会/#/?_k=dlo2cz在地址中显示类似的内容.每次刷新页面或切换到其他路径时,随机查询字符串值都会更改.

在此输入图像描述

代码被复制并粘贴在react-router分支上1.0.0-rc1.

import React from 'react';
import { Router, Route, Link, IndexRoute } from 'react-router';


const App = React.createClass({
  render() {
    return (
      <div>
        <h1>App</h1>
        {/* change the <a>s to <Links>s */}
        <ul>
          <li><Link to="/about">About</Link></li>
          <li><Link to="/inbox">Inbox</Link></li>
        </ul>

        {/*
          next we replace `<Child>` with `this.props.children`
          the router will figure out the children for us
        */}
        {this.props.children}
      </div>
    )
  }
});

const Message = React.createClass({
  render() {
    return <h3>Message</h3>
  }
});
const About = React.createClass({
  render() {
    return <h3>About</h3>
  }
});

const Inbox = React.createClass({
  render() {
    return (
      <div>
        <h2>Inbox</h2>
        {/* Render the child route component */}
        {this.props.children || "Welcome to your Inbox"}
      </div>
    )
  }
})


// Finally, we render a <Router> with some <Route>s.
// It does all the fancy routing stuff for us.
React.render((
  <Router>
    <Route path="/" component={App}>
      <Route path="about" component={About} />
      <Route path="inbox" component={Inbox}>
        {/* Add the route, nested where we want the UI to nest */}
        <Route path="messages/:id" component={Message} />
      </Route>
    </Route>
  </Router>
), document.body);
Run Code Online (Sandbox Code Playgroud)

Wit*_*ult 8

要避免这种情况,您可以设置queryKeyfalse在创建browserHistory.以下示例说明了这一点

import { Router, Route, BrowserHistory } from 'react-router';

let bHistory = BrowserHistory({
  queryKey: false
});

  <Router history={bHistory}>
    <Route path="/" component={App}>
      <Route path="about" component={About} />
      <Route path="inbox" component={Inbox}>
        {/* Add the route, nested where we want the UI to nest */}
        <Route path="messages/:id" component={Message} />
      </Route>
    </Route>
Run Code Online (Sandbox Code Playgroud)

对于React-router v2.0.0

import { Router, useRouterHistory } from 'react-router'
import { createHashHistory } from 'history'
const appHistory = useRouterHistory(createHashHistory)({ queryKey: false })
<Router history={appHistory}/>
Run Code Online (Sandbox Code Playgroud)

更新:

使用当前的React-router版本,您无需单独安装历史记录 npm模块.在安装react-router时,它将自动安装为依赖项.

如果你收到这样的警告:

Warning: Using { queryKey: false } no longer works. Instead, 
just don't use location state if you don't want a key in your URL query string.
Run Code Online (Sandbox Code Playgroud)

或者queryKey : false不工作.

然后可能是您可能与react-router有不兼容的历史模块版本.只需检查您是否已单独安装历史记录模块,如果是这种情况,请将其卸载.以上警告将消失.

编辑:获取确切的依赖项

如果你想知道你的"react-router"需要哪些依赖项,请检查github上的package.json,或者你可以尝试下面的命令.

$ npm info "react-router@2.8.1" dependencies

{ 
   history: '^2.1.2',
  'hoist-non-react-statics': '^1.2.0',
   invariant: '^2.2.1',
   warning: '^3.0.0',
  'loose-envify': '^1.2.0' 
}
Run Code Online (Sandbox Code Playgroud)


Eel*_*lke 5

它是对位置状态的引用,它在此处记录:如果想要摆脱它,您需要为历史记录提供不同的存储,例如浏览器History API,例如:

import createBrowserHistory from 'history/lib/createBrowserHistory';    
<Router history={createBrowserHistory()}>
Run Code Online (Sandbox Code Playgroud)