I’m trying to get a TouchableOpacity to get focus on press of a button. To do so I tried to use refs. It works on a web browser but not an Android device - the focus is never called.
import React, { useRef, useState } from 'react';
import {Text, TouchableOpacity,View, findNodeHandle, Button} from 'react-native';
export default function App() {
const [focused, setFocused] = useState(0);
const touchableRef = useRef();
const setFocusedElement = () => {
setFocused(1);
};
return (
<View>
<TouchableOpacity ref={touchableRef} onFocus={() => setFocusedElement()}>
<Text>Hello, I should be focused. {focused}</Text>
<Button title="try" onPress={() => touchableRef.current.focus()} />
</TouchableOpacity>
</View>
);
}
Here is a snack showing the issue. You can see that it focuses on web but not on an Android device. frisky waffle2 - Snack
I also opened a bug report on RN github… Programmatically focusing a touchable works on web, not on Android · Issue #32121 · facebook/react-native · GitHub
Thanks ahead for helping.