jestem nieco zdezorientowany, jaka jest różnica pomiędzy tym sintax:Reagować, jak poprawnie usunąć listę uczestników w componentWillUnmount, dlaczego potrzebuję powiązania w konstruktorze?
constructor(props) {
super(props);
this.state = {
openPane: false
}
this.togglePaneHelper = this.togglePaneHelper.bind(this);
}
componentDidMount() {
document.body.addEventListener('click', this.togglePaneHelper);
}
componentWillUnmount() {
document.body.removeEventListener('click', this.togglePaneHelper);
}
i ten:
constructor(props) {
super(props);
this.state = {
openPane: false
}
}
componentDidMount() {
document.body.addEventListener('click', this.togglePaneHelper.bind(this));
}
componentWillUnmount() {
document.body.removeEventListener('click', this.togglePaneHelper);
}
Moim problemem jest to, że druga składnia nie jest poprawnie i usunięciu listner powodować to ostrzeżenie:
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.
Więc pierwsze podejście to to, które poprawnie usuwa detektor zdarzeń? Wszelkie uwagi na temat tego artykułu: https://gist.github.com/Restuta/e400a555ba24daa396cc? – claireablani
Tak, pierwsze podejście działa zgodnie z zamierzeniami - tak samo, jak to jest omawiane w tym punkcie. –