Answer the question
In order to leave comments, you need to log in
Questions about one line of React code?
Hello. I'm learning JS and React, and I want to understand one line of code that I don't really understand.
There is a code that implements tabs:
const items = [
{content: 'London'},
{content: 'Paris' }
];
class Content extends React.Component {
render() {
return (
<div className="content">
<p>{this.props.content}</p>
</div>
);
}
}
class Tabs extends React.Component {
state = {
active: 0,
}
open = (e) => {
this.setState({
active: +e.target.dataset.index,
});
}
render() {
return (
<div>
<div className="tab">
{this.props.items.map((n, i) => (
<button onClick={this.open} data-index={i}>{n.content}</button>
))}
</div>
{this.props.items[this.state.active] && <Content {...this.props.items[this.state.active]} />}
</div>
);
}
}
ReactDOM.render(<Tabs items={items} />, document.getElementById('root'));
{this.props.items[this.state.active] && <Content {...this.props.items[this.state.active]} />}
&&
we write before the operator not just this.state.active
a this.props.items[this.state.active]
? What is it for? 2) Why is it not written simply &&
<Content atr={this.props.items} />
&&
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question