16

Looks very strange, when I open the /, the browser will display something like /#/?_k=dlo2cz in the address. The random query string value changes every time when I refresh the page or switch to other route.

enter image description here

The code were copied and pasted and on react-router branch 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);

2 Answers 2

8

To avoid that you can set queryKey to false while creating browserHistory. Following example illustrates that

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>

For React-router v2.0.0

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

Update:

With current React-router version you don't need to install history npm module separately. It will be automatically installed as dependency while installing react-router.

If you get warning like this:

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.

or queryKey : false is not working.

Then It could be the case that you may be having incompatible version of history module with react-router. Just check if you have installed history module separately, if that is the case then uninstall it. Above warning will go away.

Edit: To get the exact dependencies

If you want to know which dependencies your "react-router" needs check the package.json on github or you can try following command.

$ npm info "[email protected]" 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' 
}
Sign up to request clarification or add additional context in comments.

9 Comments

When I try this, it tells me Uncaught TypeError: browserHistory is not a function. Any ideas?
@denislexic Which version of react-router you are using?
How to avoid this in v2.4? It throws an error - 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
I get a blank page with this.
@saishreb Updated answer please check.
|
5

It's a reference to the location state, it's documented here: If want to get rid of it, you need a different storage for your history such as the browsers History API, for example:

import createBrowserHistory from 'history/lib/createBrowserHistory';    
<Router history={createBrowserHistory()}>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.