简体   繁体   English

React Native-通过onPress传递字符串或变量

[英]React Native - Passing Strings or Variables via onPress

This probably doesn't just apply to React Native, but I do want to understand what's happening here. 这可能不仅仅适用于React Native,但我确实想了解这里发生了什么。

Take the following 5 lines of code. 采取以下5行代码。 Line 3 will cause the app not to render in Expo. 第3行将导致该应用无法在Expo中呈现。

<Button onPress={this.filterOfficeTypePitt} title="Pitt" />
<Button onPress={this.filterOfficeTypeAtl} title="Atl" />
<Button onPress={this.filterOfficeType("pitt")} title="DOES NOT WORK" />
<Button onPress={(e) => this.filterOfficeType("pitt")} title="Pitt" />
<Button onPress={(e) => this.filterOfficeType("atl")} title="Atl" />

I somewhat understand what is going on in each line. 我有点理解每一行的情况。

Line 1 and 2: Straight forward call to a method within the component. 第1和2行:直接调用组件中的方法。

Line 3: Trying to do the same thing and pass a value. 第3行:尝试做同样的事情并传递一个值。 This causes the application to crash. 这会导致应用程序崩溃。 Reason is maximum depth exceeded. 原因是超过了最大深度。

Line 4 and 5: I think this is a function passing an anon function, and suddenly allowing me to add in a string value. 第4和5行:我认为这是一个传递anon函数的函数,突然间我可以添加一个字符串值。

Once I used 4 and 5, I was able to use a single method and have it filter the list. 一旦使用4和5,我就可以使用一种方法来过滤列表。 When I was using 1 and 2, I had to have a unique method for each. 当我使用1和2时,我必须为每种方法都有一个独特的方法。

I just want to understand what it going on better and why exactly #3 will not work. 我只想了解它会发生什么,以及为什么恰好#3无法正常工作。 I'm sure I need at least a better understanding of arrow functions. 我确定我至少需要对箭头功能有更好的了解。

Including the code for the helper function. 包括辅助函数的代码。 It basically grabs data from an index array and pushes it into a FlatList component. 它基本上从索引数组中获取数据,并将其推入FlatList组件。

filterOfficeType(officetype){
  let newarray = [];
  this.state.fulldataSource.forEach((element, index) => {
    if(element.office === officetype) {
      newarray.push(element);
    }
  });
  this.setState({
    dataSource:newarray,
    office:officetype
  });
}

filterOfficeTypePitt = () => {
  this.filterOfficeType("pitt");
}
filterOfficeTypeAtl = () => {
  this.filterOfficeType("atl");
}

Line 3 is executing the function and trying to assign the result of that to onPress prop. 第3行正在执行该函数,并尝试将其结果分配给onPress prop。 It never gets there(Why? : explained below) 它永远不会到达那里(为什么?:在下面解释)

const Button = {
   onPress: this.filterOfficeType("pitt")
}

Note: function is being called at the creation of Button object. 注意:函数在创建Button对象时被调用。

whereas the other lines are assigning the function to the onPress prop 而其他行将功能分配给onPress属性

const Button = {
   onPress: this. filterOfficeTypePitt
}

or 要么

const Button = {
   onPress: (e) => {
      this.filterOfficeType("pitt")
   }
}

Note: the function is not being called a the Button object creation rather when something presses that button 注意:该函数不被称为Button对象的创建,而是当有人按下该按钮时

And Why Line 3 causes the application to crash is because, it triggers a state change by calling setState . 第3行为何导致应用程序崩溃的原因是,它通过调用setState触发状态更改。 When setState is called, react will call render() again. 调用setState ,react将再次调用render() But this render() will execute the function again and this will call setState and react will call render() again and so the maximum depth exceeded and crash 但是此render()将再次执行该函数,这将调用setState并作出反应将再次调用render() ,因此超出最大深度并崩溃

The main difference between arrow functions and normal functions is the this scope. 箭头功能和普通功能之间的主要区别在this范围。 In arrow functions this refers to its parent object, where as in normal function this refers to itself. 在箭头函数中, this引用其父对象,而在正常函数中, this引用自身。 You can read more about arrow functions 您可以阅读有关箭头功能的更多信息

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

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