I want to get the vertical offset of a React Native TextInput in the onFocus event for that element. I have the following code in a TextInputCustom
component I created:
let textInput;
<TextInput
onFocus={(evt) => {
textInput.measure((x, y, width, height, pageX, pageY) => {
console.log('textInput.measure', x, y, width, height, pageX, pageY);
});
}}
ref={(ref) => textInput = ref}
/>
This "works", in that I can see a value for pageY
, but the problem is that when I add multiple TextInputCustom
components to a screen, it seems to report out the same pageY
value for all of them, as if the ref
is shared by all of them or something like that. (Honestly, I'm not sure what's going on, but I'm sure that they're all reporting out the same pageY
value, even though they're all clearly at differing vertical offsets.)
I was thinking about changing textInput.measure
to evt.target.measure
, but evt.target
seems to be an element ID and not an actual element. I read some stuff on SO about using ReactNativeComponentTree
to get the actual element from the element ID, but I can't seem to properly require/import it.
At this point I'm stuck, and looking for any advice on how to properly get the vertical offset of a TextInput
element when it's focused on, even when there are multiple TextInput
elements on a given screen. Thank you.
Edit: Max, as per your comments, I modified my functional component as follows:
const TextInputCustom = (props) => {
const textInputElem = useRef(null);
<TextInput
onFocus={(evt) => {
textInputElem.current.measure((x, y, width, height, pageX, pageY) => {
console.log('textInput.measure', x, y, width, height, pageX, pageY);
});
}}
ref={textInputElem}
/>
};
I've noticed that the onFocus
event fires for the first TextInputCustom
component on the screen, but none of the others. Any thoughts? Thank you.