简体   繁体   English

在jsx中使用()=> vs this.something

[英]Using () => vs this.something in jsx

I am trying to comprehend when do we use something like in jsx of our react 我正在尝试理解我们何时在反应的jsx中使用类似的东西

  1. something = {this._onRefresh}
  2. something = {() => this._onRefresh}
  3. something = {this._onRefresh()}

Where something could be something we are passing to our 其中something可能是一些我们传递给我们的

  1. child component 子组件
  2. onChange Event onChange事件
  3. onClick event onClick事件
  4. Any other situation you can think about 您可以考虑的其他情况

Where our ._onRefresh() could be 我们的._onRefresh()可能在哪里

._onRefresh = () => {
  //changes state of our react component 
  //Call redux action
  //call other functions
}

or In case of forms, it takes in event which triggered it 或如果是表格,则以触发它的事件为准

  ._onRefresh = (event) => {
      //takes target value to search inside the data 
      //Store data in the backend
    }

Can someone please explain me when do we need to use which one? 有人可以解释一下我们什么时候需要使用哪个? will help me a lot in clearing my fundamentals of react and javascript 将对我的React和JavaScript基础有所帮助

The points 1/2 and 3 are actually totally different. 1/2和3点实际上完全不同。

Your point 3 executes a function and assigns the returning value to the something . 第三点执行一个函数,并将返回值分配给something

Your examples 1/2 assign a function to something 您的示例1/2将函数分配给something

The case 3 could be used when for example you have the disable attribute, and you want to assign the returning true/false of a function. 例如,当您具有disable属性并且想要分配函数的返回true/false时,可以使用情况3。

The points 1 and 2 are assigning a function to the attribute, like for example with onClick attribute, which accepts a callback to be called on click. 点1和2为属性分配了一个函数,例如具有onClick属性的函数,该函数接受在单击时调用的回调。

The difference between the first and the second point is that if you put that code inside the render method, the second point creates a function for every render, which is not the best for performances. 第一点与第二点之间的区别在于,如果将代码放入render方法中,则第二点将为每个渲染创建一个函数,这并不是最佳性能。

Using the first point, you should pay attention on how you define the method for the reference of this inside the method. 使用第一点,你应该对你如何定义的参考方法要注意this方法里面。

If you define the class method as: 如果将类方法定义为:

myMethod() {
  console.log(this); // it will be undefined by default
}

Then you need to bind the this inside the constructor if you want to access this inside the method: 然后,如果要在方法内部访问this则需要在构造函数内绑定this

this.myMethod = this.myMethod.bind(this);

If you define the method as arrow function, it will keep the value of the this inside the method, so no need of the bind: 如果将方法定义为箭头函数,则它将this的值保留在方法内部,因此不需要绑定:

myMethod = () => {
  console.log(this);
};

1- you are passing a function as a property to this component 1-您正在将函数作为属性传递给此组件

2- you are creating a new function and passing it as a property to this component 2-您正在创建一个新函数并将其作为属性传递给此组件

3- you are passing the result (output) of calling _onRefresh as a property to this component 3-您正在将调用_onRefresh的结果(输出)作为属性传递给此组件

Option 1 is valid if _onRefresh is actual callback function that should be passed via a prop: 如果_onRefresh是应通过prop传递的实际回调函数,则选项1有效。

_onRefresh = () => console.log('refresh');

...

<Component onRefresh={this._onRefresh}/>

Where Component uses onRefresh like: Component使用onRefresh

// do refresh
props.onRefresh();

Option 2 is valid if _onRefresh is actual callback function, and onRefresh prop is expected to be higher-order function by a component that accepts a callback: 如果_onRefresh是实际的回调函数,且接受接收回调的组件的onRefresh为高阶函数 ,则选项2有效。

_onRefresh = () => () => console.log('refresh');

...

<Component onRefresh={() => this._onRefresh}/>

Where Component handles onRefresh like: Component处理onRefresh

// do refresh
const refreshFn = props.onRefresh();
refreshFn();

Option 3 is valid if _onRefresh is higher-order function that returns another function, and this is expected by a component that accepts a callback: 如果_onRefresh是返回另一个函数的高阶函数 ,并且接受回调的组件希望这样做,则选项3有效。

_onRefresh = () => () => console.log('refresh');

...

<Component onRefresh={this._onRefresh()}/>

Where Component handles onRefresh like: Component处理onRefresh

// do refresh
const refreshFn = props.onRefresh();
refreshFn();

Scenarios in options 2 and 3 are much less probable because they don't have much uses in this particular case in React. 选项2和3中的场景的可能性要小得多,因为它们在React的这种特殊情况下用处不大。

In this case option 1 is likely correct because _onRefresh does not return another function and is not expected by child component. 在这种情况下,选项1可能是正确的,因为_onRefresh不会返回另一个函数,并且子组件也不希望该函数。 Option 2 is a mistake that will result in _onRefresh being never called. 选项2是一个错误,它将导致永不调用_onRefresh Option 3 is a mistake that will result in _onRefresh being called instantly and undefined is not a function error later or, even worse, erroneous behaviour with no error. 选项3是一个错误,将导致_onRefresh立即被调用,并且undefined is not a function更高版本undefined is not a function错误,更糟糕的是,没有错误的错误行为。

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

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