简体   繁体   English

onClick函数正在另一个事件侦听器上运行

[英]onClick function is running on another event listener

Have been playing around with react. 一直在玩弄反应。 Have two event listeners the input which listens onChange and the button which should push the value to the array when its clicked. 有两个事件侦听器,其输入侦听onChange的输入和单击该值时应将值推送到数组的按钮。

Here's the code: 这是代码:

import React, { Component } from 'react';
let arr = [];
class App extends Component {
  constructor() {
    super();
    this.state = {text: 'default'}
  }
  update( e ) {
    this.setState({text: e.target.value})
  }
  add ( value ) {
    arr.push(value)
    console.log(arr)
  }
  render() {
    return (
    <div>
      <h1>{this.state.text}</h1> 
      <input onChange={this.update.bind(this)}/>
      <button onClick={this.add(this.state.text)}>Save</button>
    </div>
    )
  }
}

export default App

The problem that the add function is running on change. 添加功能正在更改时运行的问题。 Can't really get why. 真的不知道为什么。

Any suggestions? 有什么建议么?

Change <button onClick={this.add(this.state.text)}>Save</button> To <button onClick={() => this.add(this.state.text)}>Save</button> <button onClick={this.add(this.state.text)}>Save</button><button onClick={() => this.add(this.state.text)}>Save</button>

In your variant function add firing when component is rendering, and when you call setState with onChange of input you call this re-render. 在您的变体函数中,在渲染组件时添加触发,并在使用输入的onChange调用setState时调用此重新渲染。

  1. onChange() triggers update() onChange()触发update()
  2. update() calls this.setState() which changes state. update()调用this.setState()来更改状态。
  3. A state change causes render() to be invoked to re-render according to new state. 状态更改导致render()被调用以根据新状态重新渲染。
  4. Rendering <button onClick={this.add(this.state.text)}>Save</button> invokes add() every time render() runs. 渲染<button onClick={this.add(this.state.text)}>Save</button>每次运行render()都会调用add()

In order to defer invoking add() , you can define a function which gets triggered by the click event, as was shown in another answer. 为了推迟调用add() ,您可以定义一个由click事件触发的函数,如另一个答案所示。 Alternatively, you can achieve the same functionality by adding a class method which encapsulates the trigger functionality: 另外,您可以通过添加封装触发功能的类方法来实现相同的功能:

addText() {
  this.add(this.state.text)
}
render() { 
   …
   <button onClick={this.addText.bind(this)}>Save</button> 

This may or may not work for you, but in the context of the example, given, this would work. 这可能会或可能不会为您工作,但在给出的示例上下文中,这会工作。

The problem is add(this.state.text) is called whenever render() is called. 问题是无论何时调用render()都将调用add(this.state.text) To avoid this, you do not need to send the state as parameter, all you need to do is 为避免这种情况,您不需要将状态作为参数发送,您所要做的就是

<button onClick={this.add}>Save</button

or if you want to send a parameter you should bind it 或者如果您想发送参数,则应将其绑定

<button onClick={this.add.bind(this, this.state.text)}>Save</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM