Reputation: 4502
I would like to pass value of the props when user click on a particular topic which is "this.props.topicID" to other components. However, its giving undefined error when I try to access from other components. How can we set the props scope to global in order to access from other components.
var MainContainer = React.createClass({
render: function() {
return (
<div className="container">
<TopicsList />
</div>
);
}
});
var TopicsList = React.createClass({
getInitialState: function () {
return {
isTopicClicked : false,
topicPages
};
},
onClick: function (event) {
this.setState({isTopicClicked : true});
},
render: function () {
return (
<div>
<div className="row topic-list">
<SingleTopicBox
topicID="1"
onClick={this.onClick}
label="Topic"
/>
<SingleTopicBox
topicID="2"
onClick={this.onClick}
label="Topic"
/>
<SingleTopicBox
topicID="3"
onClick={this.onClick}
label="Topic"
/>
<SingleTopicBox
topicID="4"
onClick={this.onClick}
label="Topic"
/>
</div>
<div className="row">
{ this.state.isTopicClicked ? <SelectedTopicPage topicPages={topicPages} /> : null }
</div>
</div>
);
}
});
var SingleTopicBox = React.createClass({
render: function () {
return (
<div>
<div className="col-sm-2">
<div onClick={this.props.onClick.bind(null, this)} className="single-topic" data-topic-id={this.props.topicID}>
{this.props.label} {this.props.topicID}
</div>
</div>
</div>
);
}
});
var topicPages = [
{
topic_no: '1',
topic_page_no: '1',
headline: 'Topic 1 headline',
description: 'Topic 1 description comes here...',
first_topic_page: true,
last_topic_page: false
},
{
topic_no: '2',
topic_page_no: '2',
headline: 'Topic 2 headline',
description: 'Topic 2 description comes here...',
first_topic_page: false,
last_topic_page: false
},
{
topic_no: '3',
topic_page_no: '3',
headline: 'Topic 3 headline',
description: 'Topic 3 description comes here...',
first_topic_page: false,
last_topic_page: false
},
{
topic_no: '4',
topic_page_no: '4',
headline: 'Topic 4 headline',
description: 'Topic 4 description comes here...',
first_topic_page: false,
last_topic_page: true
}
];
var SelectedTopicPage = React.createClass({
render: function() {
return (
<div>
{this.props.topicPages.filter(function(topicPage) {
return topicPage.topic_no === '2'; // if condition is true, item is not filtered out
}).map(function (topicPage) {
return (
<SelectedTopicPageMarkup headline={topicPage.headline} key={topicPage.topic_no}>
{topicPage.description}
</SelectedTopicPageMarkup>
);
})}
</div>
);
}
});
var SelectedTopicPageMarkup = React.createClass({
render: function() {
return (
<div className="topics-page">
<h1>{this.props.headline}</h1>
<p>{this.props.children}</p>
</div>
);
}
});
ReactDOM.render(<MainContainer />, document.getElementById('main-container'));
Upvotes: 0
Views: 110
Reputation: 16705
It looks like the onClick
method of TopicsList
should actually be something like:
onClick: function (childBoxWithClick) {
this.setState({
isTopicClicked : true,
lastClickedTopicId: childBoxWithClick.props.topicID
});
},
Then the parent TopicsList
component can pass that info as a prop into it's other children.
However it's a bit wanky to push whole component up to a parent. So you might want to modify the
<div onClick={this.props.onClick.bind(null, this)} ... >
to something more specific like:
<div onClick={this.props.onClick.bind(null, this.props.topicId)} ... >
This should work fine in this example, but a more complex case may want to use a Flux construction, if the data truly needs to be globally accessed throughout the app.
Upvotes: 1