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'))
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')
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With