您需要使用动态变量而不是名称,这可以通过使用括号表示法来完成
<SwipeListView
data={this.state.listViewData}
renderItem={(data, i) => {
const name = 'childRef'+i
return (
<SingleCard
ref={component => this[name] = component}
itm={data.item}
/>
);
}}
renderHiddenItem={(data, i) => {
const name = 'childRef'+i
return (
<TouchableOpacity onPress={ () => console.log(this[name])}>
<Text> h </Text>
</TouchableOpacity>
);
}
}}
/>
还有当你使用ref
例如,在使用 HOC 创建的组件上connect
from react-redux
,大多数库都提供了一种称为getWrappedInstance
获取实际组件的引用而不是connect
成分。你可以像这样使用它
this[name].getWrappedInstance()
但最初你需要设置{withRef: true}
作为第四个参数connect
被用于SingleCard
like
connect(mapStateToProps, mapDispatchToProps, null, { withRef: true })(SingleCard);
你可以在这里读更多关于它的内容
https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options