मैं पिछले कुछ दिनों से रिएक्ट-राउटर का उपयोग कर रहा हूं और मैं इसे प्यार कर रहा हूं! एक समस्या यह है कि मेरे पास यह है कि मुझे अपने मूल घटक से राज्य को अपने बच्चे के घटक में पास करने का सबसे अच्छा तरीका नहीं मिल रहा है। मैं कई स्टैक ओवरफ़्लो और ब्लॉग पोस्ट देख रहा हूं लेकिन मुझे लगता है कि मैं क्या चाहता हूं। मैं जो खोज रहा हूं उसके बारे में यहां एक बहुत ही सरल उदाहरण दिया गया है।रिएक्ट-राउटर में माता-पिता से बच्चे के प्रोप के रूप में राज्य को पास करना?
class App extends React.Component {
constuctor(props) {
super(props);
this.state = {name:"helloworld", lastname:"world hello"};
}
render() { // SOMETHING LIKE THIS WOULD BE PREFFERED
if (this.props.children.name == "Home") {
{this.props.children myname={this.state.name}}
}
else if (this.props.children.name = "Account") {
{this.props.children anotherprop={this.state.lastname}}
}
}
}
class Home extends React.Component {
render() {
return (
{this.props.myname}
);
}
}
class Account extends React.Component {
render() {
return (
{this.props.lastname}
);
}
}
//ROuting config - (Only the routes not the config)
<Route path="/" component={App}>
<IndexRoute component={Home} />
<Route path="account" component={account} />
</Route>
जाहिर है यह मैं क्या हासिल करने की कोशिश कर रहा हूँ की एक बहुत ही सरलीकृत संस्करण है, लेकिन मुझे आशा है कि आप चित्र मिलता है।
टीएलडीआर: मैं माता-पिता से बच्चे को प्रोप के रूप में कैसे पास करूं? क्या मूल घटक के माध्यम से ऐसा करने का कोई तरीका है?
किसी भी मदद की सराहना की जाएगी!
यह इंगित करने योग्य है कि यह एक _experimental_ सुविधा है, और भविष्य में फेसबुक द्वारा * हटाया जा सकता है: https://facebook.github.io/react/docs/context.html – XtraSimplicity