I try to adapt this tabs to react. The idea is to add active class for the key clicked with handleclick function
class Tabs extends React.Component {
constructor(props) {
super(props);
this.state = { selected: 0 };
this.handleClick = this.handleClick.bind(this);
}
handleClick(key, event) {
event.preventDefault();
this.setState({
selected: key
});
}
render() {
var activeClass = this.state.selected === this.props.key ? "active" : "";
return (
<React.Fragment>
<div class="tab">
<button
key="1"
className={"tablinks" + activeClass}
onClick={this.handleClick}
>
London
</button>
<button
key="2"
className={"tablinks" + activeClass}
onClick={this.handleClick}
>
Paris
</button>
<button
key="3"
className={"tablinks" + activeClass}
onClick={this.handleClick}
>
Tokyo
</button>
</div>
<div key="1" className={"tabcontent" + activeClass}>
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>
<div key="2" className={"tabcontent" + activeClass}>
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
</div>
<div key="3" className={"tabcontent" + activeClass}>
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
</React.Fragment>
);
}
}
ReactDOM.render(<Tabs />, document.querySelector(".container"));
but I get two errors:
Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of Tabs.
and
Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of Tabs.
here is the jsfiddle link