I am using react router 4.0 version. I am trying to navigate from one component to another on button click. I have implemented react routing for this.
After implementation when i click the navigate button it is giving me error "Uncaught TypeError: this.context.router.push is not a function" and a warning
"Failed context type: Invalid context router
of type object
supplied to EmpLogin
, expected function
." Tried a lot but could not make it working. below are my component and routing implementation. Earlier i was using react router 2.0 and browserhistory.push was working.
export class EmpLogin extends React.Component {
constructor(props) {
super(props);
this.state = {
};
}
loginID(e) {
this.setState({loginID: e.target.value});
}
Password(e) {
this.setState({password: e.target.value});
}
navigate(e){
e.preventDefault();
this.context.router.push('/EmpDetails');
}
render() {
return (
<div>
<div >
<form>
<input type="text" onChange={this.loginID.bind(this)} />
<input type="password" onChange={this.Password.bind(this)} />
<div>
{this.props.children}
<button onClick={this.navigate.bind(this)}>feature</button>
</div>
</form>
</div>
</div>
);
}
}
EmpLogin.contextTypes = {
router: React.PropTypes.func.isRequired
}
export default EmpLogin;
Routing implementation -
import React from 'react';
import ReactDOM from 'react-dom';
import EmpLogin from './EmpLogin';
import {Router, Route,HashRouter} from 'react-router-dom';
import {browserHistory,hashHistory} from 'react-router';
import EmpDetails from './EmpDetails';
ReactDOM.render((
<HashRouter>
<div>
<Route exact path='/' component={EmpLogin}/>
<Route path='/Emplogin' component={EmpLogin}/>
<Route path='/EmpDetails' component={EmpDetails} />
</div>
</HashRouter>
), document.getElementById('root'));
To navigate to a different URL, use router.push . This method pushes a new entry into the history stack, so when the user clicks the browser back button they will be taken to the previous URL.
history. push() is another approach where we make use of the history props React Router provides while rendering a component. In other words, this works when the component is being rendered by React Router, bypassing the component as a Component prop to a Route.
“useHistory()” hook returns the history instance created by React Router, and history. push(“/profile/John”) adds the given URL to the history stack which results in redirecting the user to the given URL path. Similarly, you can use other methods and parameters of the history object as per your need.
The correct way to navigate using context in react-router v4:
this.context.router.history.push('/EmpDetails')
The new react-router v4 has changed the location of the push
method when accessing this.context.router
. router
now has history
and route
as properties and it is within history
that you will find the allocated method.
Also, if you are using the latest react version 15.5.0, propTypes have been removed into a separate package and you can get them with:
npm install prop-types --save
or
yarn add prop-types
This is written for documentation reasons. Other users answers did not successfully solve the issue, and as I had this issue I decided to fill it in for the convenience of future developers.
From the error it seems that it is looking for router
as an object:
Try this:
EmpLogin.contextTypes = {
router: React.PropTypes.object.isRequired
}
Also you can try transitionTo
instead of push()
along with the above method
this.context.router.transitionTo('/EmpDetails');
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