Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random query string appears in react-router

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);
like image 284
user1446870 Avatar asked Sep 20 '15 00:09

user1446870


People also ask

Is React Router deprecated?

This feature has been deprecated because the new structure of Routes is that they should act like components, so you should take advantage of component lifecycle methods instead.

What is Hashrouter in React?

A <Router> that uses the hash portion of the URL (i.e. window. location. hash ) to keep your UI in sync with the URL.


2 Answers

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' 
}
like image 60
WitVault Avatar answered Sep 24 '22 00:09

WitVault


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()}>
like image 28
Eelke Avatar answered Sep 20 '22 00:09

Eelke