Source:  Twitter logo

I have this error: Cannot read property 'query' of undefined When i navigate to /dashboard

The app.jsx is like this

import React from 'react'
import { render } from 'react-dom'
import { Router, Route, Link } from 'react-router'
import { createHistory } from 'history'

const App = React.createClass({
  getInitialState() {
    return {
      loggedIn: false
    }
  },

  render() {
    return (
      <div>
        <ul>
          <li>
            {this.state.loggedIn ? (
              <Link to="/logout">Log out</Link>
            ) : (
              <Link to="/login">Sign in</Link>
            )}
          </li>
          <li><Link to="/dashboard">Dashboard</Link> (authenticated)</li>
        </ul>
      </div>
    )
  }
})

const Dashboard = React.createClass({
  render() {

    return (
      <div>
        <h1>Dashboard</h1>
        <p>You made it!</p>
      </div>
    )
  }
})

const Login = React.createClass({

  contextTypes: {
    router: React.PropTypes.object.isRequired
  },

  getInitialState() {
    return {
      error: false
    }
  },

  render() {
    return (
        <div>Login Form goes here!</div>
    )
  }
})

function requireAuth(nextState, replace) {
  if (true) {
    replace({
      pathname: '/login',
      state: { nextPathname: nextState.location.pathname },
      query: '',
    })
  }
}

var history = createHistory()

render((
  <Router history={history}>
    <Route path="/" component={App}>
      <Route path="login" component={Login} />
      <Route path="dashboard" component={Dashboard} onEnter={requireAuth} />
    </Route>
  </Router>
), document.getElementById('main'))
  • ReactDOM v0.14.6
  • React (with addons) v0.14.6
  • react-router: 2.0.0-rc5 (kept from umd directory under node_modules/react-router)

Any suggestions?

Update: The problem is comming when replace function is called

I believe the issue is your require auth function.

React router has an example of auth here, https://github.com/rackt/react-router/tree/latest/examples/auth-flow

I would refactor the function like so.

function requireAuth(nextState, replaceState) {
    if (true)
        replaceState({ nextPathname: nextState.location.pathname }, '/login')
}
9 users liked answer #0dislike answer #09
Daniel profile pic
Daniel

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).