简体   繁体   English

React组件回调实现方法有什么区别

[英]React component what's the difference between callbacks implement methods

 import React from 'react'; import ChildComponent from './ChildComponent'; class SampleComponent extends React.Component { sampleCallbackOne = () => { // does something }; sampleCallbackTwo = () => { // does something }; render() { return ( <div> <ChildComponent propOne={this.sampleCallbackOne} propTwo={() => this.sampleCallbackTwo()} /> </div> ); } } export default SampleComponent;

In this example, I have an onClick event that I am handling and saw that I can successfully pass this into the props of the component in two ways.在这个例子中,我有一个正在处理的 onClick 事件,我看到我可以通过两种方式成功地将它传递到组件的 props 中。

I was wondering what exactly the difference is in both ways since they appear to function in the same manner?我想知道这两种方式到底有什么区别,因为它们似乎以相同的方式运行?

Why do both ways work?为什么这两种方式都有效?

It is a common point that seems weird.这是一个看起来很奇怪的共同点。

Refer details in document of handling-events请参阅处理事件文档中的详细信息

// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);

handleClick() {
  console.log('this is:', this);
}

<button onClick={this.handleClick}>

If you don't add () behind this.handleClick , you need to bind this in your constructor, otherwise, you may want to use the next two methods:如果你没有在this.handleClick后面添加() ,你需要在你的构造函数中绑定this ,否则你可能想使用下面两个方法:

A. public class field syntax A.公共类字段语法

which is enabled by default in Create React AppCreate React App 中默认启用

handleClick = () => {
  console.log('this is:', this);
}

<button onClick={this.handleClick}>

B. arrow functions B. 箭头函数

which may cause performance problems and is not recommended, refer to the document above.这可能会导致性能问题,不推荐使用,请参阅上面的文档。

// The same on event handling but different in:
<button
  onClick={(e) => this.deleteRow(id, e)} // automatically forwarded, implicitly
/>
<button
  onClick={this.deleteRow.bind(this, id)} // explicitly
/>

Sample样本

Basically in our practice, we use public class field syntax with params which would look like below:基本上在我们的实践中,我们使用带有参数的公共类字段语法,如下所示:

// No need to bind `this` in constructor
// Receiving params passed by elements as well as getting events of it
handler = (value: ValueType) => (event: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement>) => {
  // Do something with passed `value` and acquired `event`
}
<NumberFormat
  ...
  onBlur={this.handler(someValue)} // Passing necessary params here
/>

We can share the handler function by passing different params to it.我们可以通过向它传递不同的参数来共享handler function

// Justify via keyword of stored content in flat data structure
handler = (value: string) => (event: React.ChangeEvent<HTMLInputElement>, id: ValidationItems) => {
  // Do something with 
  // passed `value`, 
  // acquired `event`,
  // for each element diffenced via `id`
};

<YourComponent
  id="ID_1"
  value={store.name1}
  onChange={this.handler("name1")}
/>;

<YourComponent
  id="ID_2"
  value={store.name2}
  onChange={this.handler("name2")}
/>;

// ... more similar input text fields
 <ChildComponent
            propOne={this.sampleCallbackOne}
            propTwo={() => this.sampleCallbackTwo()}
          />

for propOne: here you are passing the reference of sampleCallbackOne .对于 propOne:这里你传递了sampleCallbackOne的引用。

for propTwo: you are wrapping your sampleCallbackTwo in another function.对于 propTwo:您将sampleCallbackTwo包装在另一个函数中。

In both the case you will get the same results在这两种情况下,您都会得到相同的结果

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

相关问题 React:返回 JSX 的 function 和 function 组件有什么区别? - React: what's the difference between a function returning JSX and function component? React 组件的阶段和 React 组件的生命周期有什么区别? - What is the difference between React component’s phases and React component’s life cycle? 这些命名空间方法有什么区别? - What's the difference between these namespacing methods? 这种服务声明方法有什么区别? - What's the difference between this services declaration methods? 在MooTools中,.implement和.prototype之间的区别是什么 - In MooTools, what's the difference between .implement and .prototype 使用回调和调用方法之间的区别 - Difference between using callbacks and calling methods React 组件中的 key 和 id 有什么区别? - What is the difference between key and id in React component? function 和功能性 React 组件有什么区别? - What is the difference between function and functional React component? 在 React 组件中, foo(){} 和 bar = () =&gt; {} 之间有什么区别,我什么时候应该使用哪个? - In a React Component, what's the difference between foo(){} and bar = () => {} and when should I use which? React.FunctionComponent 和普通的 JS 函数组件有什么区别? - What's the difference between a React.FunctionComponent and a plain JS function component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM