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'));