Thursday 13 June 2019

javascript - Change state in forEach loop

How can I change state on click event on the buttons? Now I have error



Uncaught TypeError: this.setState is not a function



I know that I can't use here this.setState but I can't understand where should I make bind




class Popup extends React.Component {
constructor(props){
super(props);
this.state = {opened: false};
}

componentDidMount(){
var popupOpenBtn = document.querySelectorAll('[data-popup]');

popupOpenBtn.forEach(function(item) {

item.addEventListener("click", function(){
this.setState({
opened: true
});
})
});
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...