反应onChange函数。什么都没有像事件那样通过?

时间:2018-10-12 18:38:13

标签: reactjs

我正在做一个反应教程,我看到以下代码:

enter image description here

因此,我想我们只需要向<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> .collapsible { background-color: #777; color: white; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: left; outline: none; font-size: 15px; } .active, .collapsible:hover { background-color: #555; } .content { padding: 0 18px; overflow: hidden; transition: max-height 0.2s ease-out; background-color: #f1f1f1; } </style> </head> <body> <h2>Animated Collapsibles</h2> <p>A Collapsible:</p> <button class="collapsible">Open Collapsible</button> <div class="content"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> <p>Collapsible Set:</p> <button class="collapsible">Open Section 1</button> <div class="content"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> <button class="collapsible">Open Section 2</button> <div class="content"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> <button class="collapsible">Open Section 3</button> <div class="content"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> <script> var coll = document.getElementsByClassName("collapsible"); var i; for (i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = this.nextElementSibling; if (content.style.maxHeight){ content.style.maxHeight = null; } else { content.style.maxHeight = 0 + "px"; } }); } </script> </body> </html> 字段提供一个函数即可,而不是实际调用它。这就是为什么当函数本身被传递到onChange时我们不需要将event传递给函数吗? onChange叫btw是什么?它在onChange中有名字吗?

我想我们传递给react的任何函数,React都会尝试将事件传递给它吗?

1 个答案:

答案 0 :(得分:1)

是的。基本上,React会在原始事件(与原始事件具有几乎相同的API)上创建自己的SyntheticEvent包装器,并将其作为第一个参数传递给事件处理程序。另外,传递函数而不调用它是正确的,React会在事件发生时为您调用它。