export class News extends React.Component {
constructor(props) {
super(props)
this.state = {
news: [],
}
this.deleteSingleNews =this.deleteSingleNews.bind(this)
}
componentDidMount() {
fetch('/getNews').then((response => response.json())).then(result => {
console.log('what?')
this.setState({ news: result })
})
}
deleteSingleNews(id) {
const news = this.state.news.filter(i => id !== i._id)
this.setState({news:news})
}
render() {
return (
<Container>
<Row>
{this.state.news
? this.state.news.map((item, index) => (
<Col xs={12} md={6}>
<Thumbnail
props={item} deleteItem={this.deleteSingleNews}
key={index}
/>
</Col>
))
: (<span>no data</span>)}
</Row>
</Container>
)
}
}
This is being called from child component:
const decision =()=>{
this.props.deleteItem(this.state._id)
handleClose();
}
deleteSingleNews is passed as an props to it's child component and gets executed when used clicks a button as decision. Id is passsed to the function deleteSingleNews(id) where the state is set to get rid of that particular item, right item is being removed from the array this.state.news, but the problem is view renders that the last from list is being removed.
Also, if i console log length after item removal this.state.news.length it shows the original length, unless i console log it in callback.