【发布时间】:2020-01-25 07:28:46
【问题描述】:
我仍然无法理解 React Native(以及一般的 React)中的 ref。我正在使用功能组件。我有一个包含许多项目的 FlatList。如何为文本或视图组件等项目中的事物创建引用?
<FlatList
data={data}
renderItem={({ item }} => {
<View>
... lots of other stuff here
<TouchableOpacity onPress={() => _editITem(item.id)}>
<Text ref={(a) => 'text' + item.id = a}>EDIT</Text>
</TouchableOpacity>
</View>
}
/>
然后在_editItem 中,我想引用 Text 组件,以便我可以将其文本从“EDIT”更改为“EDITING”,甚至更改其样式或其他任何内容。
_editPost = id => {
console.log(text + id)
}
我试过了……
FeedComponent = () => {
let editPosts = {}
<FlatList
data={data}
renderItem={({ item }} => {
<View>
... lots of other stuff here
<TouchableOpacity onPress={() => _editITem(item.id)}>
<Text ref={(a) => editPosts[item.id] = a}>EDIT</Text>
</TouchableOpacity>
</View>
}
/>
...还有一些其他的事情,但我想我可能还有一段路要走,所以我可以使用一些指导。
【问题讨论】:
标签: javascript react-native react-native-flatlist ref