如何在按钮上更改点击事件的状态?现在我有错误
未捕获的TypeError:this.setState不是函数
我知道我无法在这里使用 this.setState ,但我无法理解我应该在哪里进行绑定
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
});
})
});
}
答案 0 :(得分:2)
您的点击处理程序的范围是按钮,而不是类。试试这个:
pi@rpi:~ $ sudo apt-get install python-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libglew1.10 liborcus-0.8-0 libpython3.4 libpython3.4-dev libpython3.4-minimal libpython3.4-stdlib
libwps-0.3-3 python3.4 python3.4-dev python3.4-minimal
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libpython-dev libpython-stdlib libpython2.7 libpython2.7-dev libpython2.7-minimal libpython2.7-stdlib
python python-minimal python2.7 python2.7-dev python2.7-minimal
Suggested packages:
python-doc python-tk python2.7-doc
The following NEW packages will be installed:
libpython-dev libpython2.7-dev python-dev python2.7-dev
The following packages will be upgraded:
libpython-stdlib libpython2.7 libpython2.7-minimal libpython2.7-stdlib python python-minimal python2.7
python2.7-minimal
8 upgraded, 4 newly installed, 0 to remove and 784 not upgraded.
Need to get 32.6 MB of archives.
After this operation, 37.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.raspbian.org/raspbian stretch/main armhf python2.7 armhf 2.7.12-2 [277 kB]
Get:2 http://archive.raspbian.org/raspbian stretch/main armhf libpython2.7 armhf 2.7.12-2 [913 kB]
Get:3 http://archive.raspbian.org/raspbian stretch/main armhf libpython2.7-stdlib armhf 2.7.12-2 [1,843 kB]
Get:4 http://archive.raspbian.org/raspbian stretch/main armhf python2.7-minimal armhf 2.7.12-2 [1,173 kB]
Get:5 http://archive.raspbian.org/raspbian stretch/main armhf libpython2.7-minimal armhf 2.7.12-2 [388 kB]
Get:6 http://archive.raspbian.org/raspbian stretch/main armhf python-minimal armhf 2.7.11-2 [40.3 kB]
Get:7 http://archive.raspbian.org/raspbian stretch/main armhf python armhf 2.7.11-2 [153 kB]
Get:8 http://archive.raspbian.org/raspbian stretch/main armhf libpython-stdlib armhf 2.7.11-2 [19.8 kB]
Get:9 http://archive.raspbian.org/raspbian stretch/main armhf libpython2.7-dev armhf 2.7.12-2 [27.5 MB]
Get:10 http://archive.raspbian.org/raspbian stretch/main armhf libpython-dev armhf 2.7.11-2 [19.8 kB]
Get:11 http://archive.raspbian.org/raspbian stretch/main armhf python2.7-dev armhf 2.7.12-2 [278 kB]
Get:12 http://archive.raspbian.org/raspbian stretch/main armhf python-dev armhf 2.7.11-2 [1,132 B]
Fetched 32.6 MB in 25s (1,284 kB/s)
Reading changelogs... Done
(Reading database ... 130446 files and directories currently installed.)
Preparing to unpack .../python2.7_2.7.12-2_armhf.deb ...
Unpacking python2.7 (2.7.12-2) over (2.7.9-2) ...
Preparing to unpack .../libpython2.7_2.7.12-2_armhf.deb ...
Unpacking libpython2.7:armhf (2.7.12-2) over (2.7.9-2) ...
Preparing to unpack .../libpython2.7-stdlib_2.7.12-2_armhf.deb ...
Unpacking libpython2.7-stdlib:armhf (2.7.12-2) over (2.7.9-2) ...
Preparing to unpack .../python2.7-minimal_2.7.12-2_armhf.deb ...
Unpacking python2.7-minimal (2.7.12-2) over (2.7.9-2) ...
Preparing to unpack .../libpython2.7-minimal_2.7.12-2_armhf.deb ...
Unpacking libpython2.7-minimal:armhf (2.7.12-2) over (2.7.9-2) ...
Preparing to unpack .../python-minimal_2.7.11-2_armhf.deb ...
Unpacking python-minimal (2.7.11-2) over (2.7.9-1) ...
Processing triggers for gnome-menus (3.13.3-6) ...
Processing triggers for desktop-file-utils (0.22-1) ...
Processing triggers for mime-support (3.58) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for libc-bin (2.23-4) ...
Setting up libpython2.7-minimal:armhf (2.7.12-2) ...
Setting up python2.7-minimal (2.7.12-2) ...
Setting up python-minimal (2.7.11-2) ...
(Reading database ... 130445 files and directories currently installed.)
Preparing to unpack .../python_2.7.11-2_armhf.deb ...
Unpacking python (2.7.11-2) over (2.7.9-1) ...
Preparing to unpack .../libpython-stdlib_2.7.11-2_armhf.deb ...
Unpacking libpython-stdlib:armhf (2.7.11-2) over (2.7.9-1) ...
Selecting previously unselected package libpython2.7-dev:armhf.
Preparing to unpack .../libpython2.7-dev_2.7.12-2_armhf.deb ...
Unpacking libpython2.7-dev:armhf (2.7.12-2) ...
Selecting previously unselected package libpython-dev:armhf.
Preparing to unpack .../libpython-dev_2.7.11-2_armhf.deb ...
Unpacking libpython-dev:armhf (2.7.11-2) ...
Selecting previously unselected package python2.7-dev.
Preparing to unpack .../python2.7-dev_2.7.12-2_armhf.deb ...
Unpacking python2.7-dev (2.7.12-2) ...
Selecting previously unselected package python-dev.
Preparing to unpack .../python-dev_2.7.11-2_armhf.deb ...
Unpacking python-dev (2.7.11-2) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up libpython2.7-stdlib:armhf (2.7.12-2) ...
Setting up python2.7 (2.7.12-2) ...
Setting up libpython-stdlib:armhf (2.7.11-2) ...
Setting up libpython2.7:armhf (2.7.12-2) ...
Setting up libpython2.7-dev:armhf (2.7.12-2) ...
Setting up python2.7-dev (2.7.12-2) ...
Setting up python (2.7.11-2) ...
Setting up libpython-dev:armhf (2.7.11-2) ...
Setting up python-dev (2.7.11-2) ...
Processing triggers for libc-bin (2.23-4) ...
pi@rpi:~ $ sudo apt-get install python-lxml
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libglew1.10 liborcus-0.8-0 libpython3.4 libpython3.4-dev libpython3.4-minimal libpython3.4-stdlib
libwps-0.3-3 python3.4 python3.4-dev python3.4-minimal
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
python-bs4
Suggested packages:
python-lxml-dbg python-lxml-doc
The following NEW packages will be installed:
python-bs4 python-lxml
0 upgraded, 2 newly installed, 0 to remove and 784 not upgraded.
Need to get 761 kB of archives.
After this operation, 2,991 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.raspbian.org/raspbian stretch/main armhf python-bs4 all 4.5.0-1 [85.5 kB]
Get:2 http://archive.raspbian.org/raspbian stretch/main armhf python-lxml armhf 3.6.0-1 [675 kB]
Fetched 761 kB in 2s (301 kB/s)
Selecting previously unselected package python-bs4.
(Reading database ... 130580 files and directories currently installed.)
Preparing to unpack .../python-bs4_4.5.0-1_all.deb ...
Unpacking python-bs4 (4.5.0-1) ...
Selecting previously unselected package python-lxml.
Preparing to unpack .../python-lxml_3.6.0-1_armhf.deb ...
Unpacking python-lxml (3.6.0-1) ...
Setting up python-bs4 (4.5.0-1) ...
Setting up python-lxml (3.6.0-1) ...
pi@rpi:~ $ sudo dpkg -l |grep libxml2
ii libxml2:armhf 2.9.1+dfsg1-5+deb8u2 armhf GNOME XML library
ii python-lxml 3.6.0-1 armhf pythonic binding for the libxml2 and libxslt libraries