简体   繁体   English

onClick 从 class 更改为 function

[英]onClick change from class to function

In offical react tutorial they first write class component:在官方反应教程中,他们首先编写 class 组件:

class Square extends React.Component {
 render() {
   return (
     <button className="square" onClick={() => this.props.onClick()}>
       {this.props.value}
     </button>
   );
 }
}

and then make it a function component然后使其成为 function 组件

function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>
      {props.value}
    </button>
  );
}

Why is the onClick change necessary ie why did we remove the arrow function?为什么需要更改 onClick,即为什么我们删除了箭头 function?

In the first case when we remove the arrow function ie just keep this.props.onClick() the event happens every time the square renders without even clicking the button and why is it so?在第一种情况下,当我们删除箭头 function 即只保留 this.props.onClick() 时,每次渲染正方形时都会发生事件,甚至无需单击按钮,为什么会这样? ps:I am new to react js, got these doubts when following the tutorial,it would be great if you could give a elaborate answer and any further resources that could help.!thanks in advance. ps:我是反应js的新手,在学习教程时遇到了这些疑问,如果你能给出详细的答案和任何可以帮助的进一步资源,那就太好了。!提前谢谢。

I'm guessing you did the below?我猜你做了以下?

aka, changed又名,变了

class Square extends React.Component {
 render() {
   return (
     <button className="square" onClick={() => this.props.onClick()}>
       {this.props.value}
     </button>
   );
 }
}

^ That, to ^ 那,到

class Square extends React.Component {
 render() {
   return (
     <button className="square" onClick={this.props.onClick()}>
       {this.props.value}
     </button>
   );
 }
}

^ this ^ 这个

If so, you're basically calling this.props.onClick() everytime the component is rendered.如果是这样,您基本上是在每次渲染组件时调用this.props.onClick() The return value of this.props.onClick() would then be set as the onClick value.然后this.props.onClick()返回值将设置为 onClick 值。 This is useful for when you want code to be executed that is relative to the onClick event and then return a function that handles the actual onClick event.当您希望执行与 onClick 事件相关的代码,然后返回处理实际 onClick 事件的 function 时,这很有用。

To replicate the same as the second example, just do.要复制与第二个示例相同的内容,只需执行此操作。

class Square extends React.Component {
 render() {
   return (
     <button className="square" onClick={this.props.onClick}>
       {this.props.value}
     </button>
   );
 }
}

As the this.props.onClick function is assigned to the onClick prop not called.由于this.props.onClick function 被分配给未调用的onClick道具。

Basic Function Calling explanation基本 Function 调用说明

function onClickHandler() {
    console.log('I WAS CLICKED!'):
}

function callThis() {

   return onClickHandler;
}

var assignedOnClick = callThis(); < Calls callThis which returns onClickHandler (without calling it)

var alsoAssignedOnClick = onClickHandler;

alsoAssignedOnClick(); < Calls onClickHandler which has just been assigned to the alsoAssignedOnClick variable

Tip: Don't overthink with React or coding in general.提示:不要过度考虑 React 或一般的编码。 Pretty much all of coding is just getting data from A to B, the hard part of it is keeping your data/call stack as minimal as possible.几乎所有的编码都只是从 A 到 B 获取数据,其中最难的部分是尽可能减少数据/调用堆栈。

In the first case, they wrap the callback in charge of handling the click event in an arrow function, to ensure that ' this ' is available within the onClick function.在第一种情况下,他们将负责处理单击事件的回调包装在箭头 function 中,以确保“ this ”在 onClick ZC1C425268E68385D1AB5074C17A94 中可用。 This syntax is correct but there is a problem that a different callback is created each time your button is rendered.此语法是正确的,但存在一个问题,即每次呈现按钮时都会创建不同的回调。 In most cases this works fine, but if this callback is passed as a prop to other components, these components could be re-rendered and it can bring performance issues.在大多数情况下,这可以正常工作,但是如果将此回调作为 prop 传递给其他组件,则这些组件可能会被重新渲染,并且可能会带来性能问题。 This is a class component's behavior.这是 class 组件的行为。

The other way to ensure that ' this ' is available within the onCLick function is by placing this in your class's constructor:确保“ this ”在onCLick function 中可用的另一种方法是将其放在类的构造函数中:

this.onClick = this.props.onClick.bind(this);

and then just send the function reference as a prop to your components like this:然后只需将 function 参考作为道具发送给您的组件,如下所示:

<button onClick={this.onClick}>
    {props.value}
</button>

In the second case, that they are using functional components, what is being passed as a parameter is the reference to the function.在第二种情况下,他们正在使用功能组件,作为参数传递的是对 function 的引用。 In this case there is no 'this', therefore it makes no sense to wrap the call to your callback function in another arrow function and you cannot use bind.在这种情况下,没有“this”,因此在另一个箭头 function 中包装对回调 function 的调用是没有意义的,并且您不能使用绑定。 It would be valid to do something like this:这样做是有效的:

<button className="square" onClick={() => props.onClick()}>
    {props.value}
</button>

but unnecessary since what you would be doing is passing the reference to a function that simply invokes the function in charge of handling the event internally, so it is cleaner to pass the reference to the function directly.但没有必要,因为您要做的是传递对 function 的引用,该引用仅调用负责在内部处理事件的 function,因此直接将引用传递给 ZC1C425268E68385D1AB4A944C 会更简洁。

As @ShubhamKhatri said: "Each time render is called an anonymous function is created and that function when called, calls this.onClick. Hovewer The arrow function does not recreate function everytime." As @ShubhamKhatri said: "Each time render is called an anonymous function is created and that function when called, calls this.onClick. Hovewer The arrow function does not recreate function everytime." You can read all detail.您可以阅读所有详细信息。 here这里

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

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