React
- 2 ответа
- 0 вопросов
3
Вклад в тег
this.state.data.reply.length
равно 0, поэтому выводится 0. Попробуй так:{!!this.state.data.reply.length &&
<div className="reply">
<Posts messages={this.state.data.reply}/>
</div>
}
{this.state.data.reply.length > 0 &&
<div className="reply">
<Posts messages={this.state.data.reply}/>
</div>
}
const locations = [
{
city: "New York"
},
{
city: "Moscow"
}
];
const First = props =>
<div className="first">
<button onClick={props.handleClick}>
click
</button>
<p>{props.city}</p>
</div>;
const Second = props =>
<div className="second">
<span>
{props.city && props.city}
</span>
</div>;
class TestComponent extends React.Component {
constructor() {
super();
this.state = {
selectedCity: false,
};
}
handleClick = city => () => {
this.setState({
selectedCity: city,
});
};
render() {
return (
<div>
{locations.map((data, i) =>
<First
city={data.city}
handleClick={this.handleClick(data.city)}
/>
)}
<Second city={this.state.selectedCity} />
</div>
);
}
}
ReactDOM.render(
<TestComponent />,
document.querySelector('[data-role-id="content"]'));