I am trying to access my router
from within my component and it is undefined. Here is my router
:
React.render(
<Provider store={store}>
{() =>
<Router>
<Route path="/" component={LoginContainer} />
</Router>
}
</Provider>,
document.getElementById('app')
);
Here is the container:
class LoginContainer extends React.Component {
constructor() {
super();
}
static propTypes = {
handleLogin: PropTypes.func.isRequired
}
static contextTypes = {
router: React.PropTypes.object
}
handleLogin() {
this.props.dispatch(Actions.login(null, null, this.context.router));
}
render() {
return (
<Login
auth={this.props}
handleLogin={this.handleLogin}
/>
);
}
}
function mapStateToProps(state) {
return {
stuff: []
}
}
export default connect(mapStateToProps)(LoginContainer);
And finally the component:
import React, { PropTypes } from 'react';
class Login extends React.Component {
static propType = {
handleLogin: PropTypes.func.isRequired
}
static contextTypes = {
router: React.PropTypes.object
}
render() {
return (
<div className="flex-container-center">
<form>
<div className="form-group">
<button type="button" onClick={this.props.handleLogin}>Log in</button>
</div>
</form>
</div>
);
}
}
module.exports = Login;
When I click on the login
button, it hits the handleLogin
in the container. In my handleLogin
, my this
value is undefined
. I have tried to bind this
to the function in the constructor
, but it still is undefined
.
Also, when I put a breakpoint in my render
function, I have a this.context.router
, but it is undefined
. How do I get my this
correct in my handleLogin
and how to I make sure that I have my router
on the context
and it is not undefined
?
To enable routing in our React app, we first need to import BrowserRouter from react-router-dom . This should hold everything in our app where routing is needed. That means, if we need routing in our entire app, we must wrap our higher component with BrowserRouter .
React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL. Let us create a simple application to React to understand how the React Router works.
To add the link in the menu, use the <NavLink /> component by react-router-dom . The NavLink component provides a declarative way to navigate around the application. It is similar to the Link component, except it can apply an active style to the link if it is active.
The best way to keep up with changes is to look through the Releases page.
In React Router versions that are > 1.0.0-beta3
and < 2.0.0-rc2
, there is no context.router
. Instead, you need to look for context.history
.
If you use versions <= 1.0.0-beta3
or >= 2.0.0-rc2
, the context.router
is there. In short, what happened is it was removed in favor of history
but then the maintainters decided it’s best to hide the History library API behind the router, so they brough back the router
context in 2.0 RC2 and onward.
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