मैं अपने घटक के भीतर से अपने router
तक पहुंचने का प्रयास कर रहा हूं और यह अनिर्धारित है। यहाँ मेरी router
है:मेरा संदर्भ घटक क्यों है मेरा प्रतिक्रिया घटक में अपरिभाषित?
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);
और अंत में घटक:
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;
जब मैं login
बटन पर क्लिक करें, उस में handleLogin
हिट
React.render(
<Provider store={store}>
{() =>
<Router>
<Route path="/" component={LoginContainer} />
</Router>
}
</Provider>,
document.getElementById('app')
);
यहाँ कंटेनर है कंटेनर। मेरे handleLogin
में, मेरे this
मान undefined
है। मैंने में फ़ंक्शन में this
को बांधने का प्रयास किया है, लेकिन यह अभी भी undefined
है।
इसके अलावा, जब मैं अपने render
फ़ंक्शन में ब्रेकपॉइंट डालता हूं, तो मेरे पास this.context.router
है, लेकिन यह undefined
है। मैं अपने this
को अपने handleLogin
में सही कैसे प्राप्त करूं और मैं यह सुनिश्चित कैसे करूं कि मेरे पास router
context
पर है और यह undefined
नहीं है?
मेरे पास एक संकेत है कि यह सलाह पुरानी है। मैं प्रतिक्रिया-राउटर संस्करण 2.0.0-आरसी 5 का उपयोग कर रहा हूं और मुझे चेतावनियां मिल रही हैं जो इंगित करती हैं कि विपरीत सत्य है। यह मुझे context.history के बजाय context.router का उपयोग करने के लिए प्रेरित कर रहा है। क्या आप इस पर कुछ प्रकाश डाल सकते हैं? धन्यवाद! – Ryan
@ रयान की यही भावना थी और यह सत्यापित कर सकता है कि 'context.history' को बहिष्कृत किया गया है। 'context.router' को प्राथमिकता दी जाती है। मेरे लिए मुझे बस 'इतिहास' को 'राउटर' के साथ अपने 'संदर्भ प्रकार' में बदलना पड़ा। – sighrobot
@ साइबरोबॉट धन्यवाद, मैंने जवाब अपडेट किया। –