I'm trying to use This
inside a function on a arrow function. How can I use setState
inside my function?
showDeleteConfirm = commentId => {
const { deleteComment } = this.props;
const { comments } = this.state;
// this exist
confirm({
title: 'Are you sure delete this?',
okText: 'Yes',
okType: 'danger',
cancelText: 'No',
onOk() {
// this is undefined
deleteComment(commentId);
const targetPosition = _.findIndex(comments, item => {
return item._id === commentId;
});
if (targetPosition !== -1) {
// this is undefined
console.log(this.state.comments);
this.setState(prevState => ({
comments: [
...prevState.comments.slice(0, targetPosition),
...prevState.comments.slice(targetPosition + 1)
]
}));
}
},
onCancel() {
console.log('Cancel');
}
});
};