如何通过定义的prop函数传递附加参数?

时间:2019-04-16 03:44:43

标签: javascript react-native jsx react-native-collapsible

我需要为已定义参数的道具声明一个函数。 Component是来自react-native-collapsible的手风琴。我要实现功能的组件的属性是onChange(indexes),我想为其传递一个附加参数'title'。

我尝试传递一个具有相同名称“ indexes”的参数,但尚未声明变量“ indexes”,因此在执行时返回错误。我不知道该使用哪种语法传递新参数。

将函数传递给prop时不带任何参数就可以了,但是我需要将标题作为参数传递给函数。

组件本身(我希望onChange道具调用带有活动手风琴索引和标题的updateSections):

<Accordion
  sections={data}
  activeSections={activeSections}
  renderHeader={this.renderHeader}
  renderContent={this.renderList}
  onChange={this.updateSections(indexes, title)}
  touchableComponent={TouchableOpacity}
/>

这很好用:

<Accordion
  sections={data}
  activeSections={activeSections}
  renderHeader={this.renderHeader}
  renderContent={this.renderList}
  onChange={this.updateSections}
  touchableComponent={TouchableOpacity}
/>

要实现的功能:

updateSections = (activeSections, title) => {
  switch (title) {
   case 'PA':
     this.setState({ activeSectionsPA: activeSections });
     break;
   case 'CA':
     this.setState({ activeSectionsCA: activeSections });
     break;
   default:
     break;
  }
}

任何帮助将不胜感激!谢谢!

2 个答案:

答案 0 :(得分:0)

我认为您只需要将()=>放在前面,例如:

  onChange={() => this.updateSections(indexes, title)}

答案 1 :(得分:0)

在那里使用箭头功能

<Accordion
  sections={data}
  activeSections={activeSections}
  renderHeader={this.renderHeader}
  renderContent={this.renderList}
  onChange={(indexes) => this.updateSections(indexes, title)} // change this
  touchableComponent={TouchableOpacity}
/>