I have code at the following url: https://gist.github.com/motleydev/6d5e980e4d90cc5d52fd where I am constructing a tab type setup. There's a row of tabs, a section of content and then another row of images with an alternating active state depending on which tab is "open." What I ultimately need to do is be able to get the index of the tab clicked but I can't seem to figure out a good way to do that. All I need is just the integer. Appreciate any insight. Thanks!
-
Make another component called Tab and send the Container onClick event as a property as well as the index. Then you can invoke onClick on the actual tab where you call the Container's onClick with the correct index. Its dirty, but I think it will work.magnudae– magnudae2015-03-19 11:05:03 +00:00Commented Mar 19, 2015 at 11:05
-
I believe the answer is to store the index on the HTML element, and retrieve with e.target or e.currentTarget, not to bind the function every time the map happensneaumusic– neaumusic2017-02-16 19:42:13 +00:00Commented Feb 16, 2017 at 19:42
-
stackoverflow.com/a/20383295/1487102neaumusic– neaumusic2017-02-16 19:47:53 +00:00Commented Feb 16, 2017 at 19:47
3 Answers
I'm not able to test this currently, but something along the lines of this. Basically send a callback and an index to each individual tab. When a tab is clicked, execute the callback with the index as parameter:
var Tab = React.createClass({
handleClick: function (event) {
event.preventDefault();
this.props.tabClicked(this.props.index);
},
render: function () {
return (
<li>
<a href="#" onClick={this.handleClick}>Tab {this.props.index}</a>
</li>
);
}
});
var Tabs = React.createClass({
handleClick: function (index) {
console.log('Tab #' + index);
},
render: function(){
var self = this;
var tabs = this.props.tabs.map(function (tab, index) {
return (
<Tab key={index} index={index} tabClicked={this.handleClick} />
)
});
return (
<nav>
<ul>
{tabs}
</ul>
</nav>
);
}
});
2 Comments
data on the elementIt is possible to it do without creating a new component by using getAttribute() function of event.target and adding index as a custom attribute
var Tabs = React.createClass({
handleClick: function(event){
event.preventDefault();
console.log(event.target.getAttribute('index');
},
render: function(){
var self = this;
var tabs = this.props.tabs.map(function(tab,index){
var clickHandler = self.handleClick;
return (<li key={'tab'+index}><a ref={'tab'+index} index={index} href="#" onClick={clickHandler} > {tab.meta.title}</a></li>)
});
return (
<nav>
<ul>
{tabs}
</ul>
</nav>
)
}
});
You can find more information about event.target.getAttribute() at https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes#JavaScript_Access
Comments
Jorum solution works.
You can also replace:
handleClick: function(event){
event.preventDefault();
console.log(this);
},
render: function(){
var self = this;
var tabs = this.props.tabs.map(function(tab,index){
var clickHandler = self.handleClick;
return (<li key={'tab'+index}><a ref={'tab'+index} href="#" onClick={clickHandler} > {tab.meta.title}</a></li>)
});
return (
<nav>
<ul>
{tabs}
</ul>
</nav>
)
}
});
By:
handleClick: function(index){
event.preventDefault();
console.log(this);
},
render: function(){
var self = this;
var tabs = this.props.tabs.map(function(tab,index){
var clickHandler = self.handleClick.bind(null,index);
return (<li key={'tab'+index}><a ref={'tab'+index} href="#" onClick={clickHandler} > {tab.meta.title}</a></li>)
});
return (
<nav>
<ul>
{tabs}
</ul>
</nav>
)
}
});