【问题标题】:How to pass an additional parameter with a defined prop function?如何使用定义的 prop 函数传递附加参数?
【发布时间】:2019-09-06 02:08:44
【问题描述】:

我需要为已定义参数的道具声明一个函数。该组件是来自 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;
  }
}

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

【问题讨论】:

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


    【解决方案1】:

    在那里使用箭头功能

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

    【讨论】:

    • 谢谢,我之前没想到 :D
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-06
      • 2020-12-03
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多