Hey guys,
I’m trying to get a TouchableOpacity to get focus after render. I tried to use refs and trigger focus from useEffect. It works on a web browser but not an Android device - the focus is never called.
import React, { useRef, useEffect, useState } from 'react';
import {Text,TouchableOpacity,View} from 'react-native';
export default function App() {
const [focused, setFocused] = useState(0);
const touchableRef = useRef();
useEffect(() => {
touchableRef.current?.focus();
}, []);
const setFocusedElement = () => {
setFocused(1);
};
return (
<View>
<TouchableOpacity ref={touchableRef} onFocus={() => setFocusedElement()}>
<Text>Hello, I should be focused. {focused}</Text>
</TouchableOpacity>
</View>
);
}
Here is a snack showing the issue. You can see that it focuses on web but not on an Android device. frisky waffle - Snack
Thanks ahead