I'm getting undefined
when I try to access the ref object. I'm mapping an array of images and attempting to access individual ones through useRef.
First, I initialized the useRef as following:
const gridImages = useRef([React.createRef(), React.createRef()])
And then mapping of the images:
<View style={styles.grid}>
{images.map((src, index) => {
const style = index === activeIndex ? activeIndexStyle : undefined
return (
<TouchableWithoutFeedback key={index} onPress={() => handleOpenImage(index)}>
<Animated.Image
source={{ uri: src }}
style={[ styles.gridImage, style ]}
ref={gridImages.current[index]}
/>
</TouchableWithoutFeedback>
)
})}
</View>
When I invoke the following method, I get an error saying
undefined is not an object (evaluating 'gridImages.current[index].getNode')
When I console.log
the object gridImages.current
shows the methods with no problem, but as soon as I include the index
, I get the undefined
error.
const handleOpenImage = index => {
console.log(gridImages.current[index])
gridImages.current[index].getNode().measure((x, y, width, height, pageX, pageY) => {
position.setValue({
x: pageX,
y: pageY,
})
size.setValue({
x: width,
y: height,
})
setActiveImage(images[index])
setActiveIndex(index)
})
}