I’m coding in react native and I want to create a way for my code to automatically click on a button once the app is opened by user. I want the button to be clicked by the app automatically without the user having to click on the button themselves. My code so far is:
import React, { useRef } from 'react';
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View,Button } from 'react-native';
import {openBrowserAsync} from 'expo-web-browser'
export default function App() {
const btnRef = useRef();
return (
<View style={styles.container}>
<Button title="Open Browser" onPress={() => openBrowserAsync("https://dicateeu.co.uk")} />
<StatusBar style="auto" />
</View>
);
}
My code so far just opens a web browser but I want the app to open the web browser by itself.
2
Answers
Use useEffect-Hook. It will execute when the screen renders.
you can use the useEffect hook to trigger the button’s onPress function after the component has been mounted