首页 > 解决方案 > 如何使用定义的 prop 函数传递附加参数?

问题描述

我需要为已定义参数的道具声明一个函数。该组件是来自 react-native-collapsible 的 Accordion。我要实现一个函数的组件的 prop 是 onChange(indexes),我想为它传递一个附加参数“title”。

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

将函数传递给没有任何参数的道具就可以了,但我需要将标题作为参数传递给函数。

组件本身(我希望 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;
  }
}

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

标签: javascriptreact-nativejsxreact-native-collapsible

解决方案


在那里使用箭头功能

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

推荐阅读