我需要为具有已定义参数的 Prop 声明一个函数。该组件是来自 react-native-collapsible 的 Accordion。我想实现一个功能的组件的 prop 是 onChange(indexes),我想为它传递一个额外的参数 'title'。
我试过传递一个具有相同名称“indexes”的参数,但变量“indexes”尚未声明,因此在执行时返回错误。我不知道传递新参数的语法。
不带任何参数将函数传递给 prop 就可以了,但我需要将标题作为参数传递给函数。
组件本身(我希望 onChange 属性调用带有事件 Accordion 索引和标题的 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;
}
}
任何帮助将不胜感激!谢谢!
最佳答案
在那里使用箭头函数
<Accordion
sections={data}
activeSections={activeSections}
renderHeader={this.renderHeader}
renderContent={this.renderList}
onChange={(indexes) => this.updateSections(indexes, title)} // change this
touchableComponent={TouchableOpacity}
/>
关于javascript - 如何使用定义的 prop 函数传递附加参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55700258/