Answer the question
In order to leave comments, you need to log in
React, how does it work?
How it works ? And why does this code even work? Specifically, about the state of the component.
class News extends React.Component {
state = {
filteredNews: this.props.data,
}
componentWillReceiveProps(nextProps) {
console.log({ nextProps })
console.log({ oldProps: this.props })
}
renderNews = () => {
const { filteredNews } = this.state // используем состояние
let newsTemplate = null
if (filteredNews.length) { // везде data заменена на filteredNews
newsTemplate = filteredNews.map(function(item) {
return <Article key={item.id} data={item} />
})
} else {
newsTemplate = <p>К сожалению новостей нет</p>
}
return newsTemplate
}
render() {
const { filteredNews } = this.state // аналогично, используем состояние
return (
<div className="news">
{this.renderNews()}
{filteredNews.length ? (
<strong className={'news__count'}>
Всего новостей: {filteredNews.length}
</strong>
) : null}
</div>
)
}
}
Answer the question
In order to leave comments, you need to log in
Recording:
state = {
filteredNews: this.props.data,
};
constructor(props) {
super(props);
this.state = {
filteredNews: this.props.data,
};
}
renderNews() {
const { filteredNews } = this.state;
if (filteredNews.length) {
return filteredNews.map(item => (
<Article key={item.id} data={item} />
));
}
return <p>К сожалению новостей нет</p>;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question