反应路由器 - 无法读取未定义的属性'query'



我有这个错误: Cannot read property 'query' of undefined 当我导航到/dashboard 时

app.jsx 是这样的

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'))
  • 反应DOM v0.14.6
  • 反应(带插件)v0.14.6
  • react-router:2.0.0-rc5(保存在 node_modules/react-router 下的 UMD 目录中)

有什么建议吗?

更新:调用函数时出现问题replace

我相信

问题是您需要身份验证功能。

React 路由器在这里有一个身份验证的例子,https://github.com/rackt/react-router/tree/latest/examples/auth-flow

我会像这样重构函数。

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

最新更新