Я пытаюсь получить ссылку на элемент во время рендеринга плоского списка, но ссылка возвращается как неопределенная.См. Соответствующий код ниже:
Конструктор:
constructor(props) {
super(props);
this.modalRef = createRef();
}
RenderItem:
_renderItem = ({ item }, context) => {
const modalRef = context.modalRef.current; // Why is context.modalRef.current null?
let onPress = () => alert("Something went wrong...");
return (
<View style={styles.item}>
<ListItemCard
id={item.id}
value="Some value.."
onPress={onPress()}
/>
</View>
);
};
В функции визуализации:
Модально на экране:
<Modal
ref={this.modalRef}
text="Some text.."
onPressClose={() =>
this.modalRef.current.close()
}
/>
Плоский список:
<FlatList
data={data}
renderItem={item => this._renderItem(item, this)}
keyExtractor={(item, index) => index.toString()}
/>