I'm trying to create my first React Native App so I started with expo, but I have issues with componentDidMount.
From what I've seen, to allow the user to be prompted to allow notifications, locations, etc; Using componentDidMount will prompt the user at the start of the screen if they haven't been asked before. However, I want to prompt the user from a button press which I achieved with:
code to call _getLocation with button press
with this being _getLocation: _getLocation code
I can do the same thing with _getNotification and just call it on onPress within TouchableOpacity.
But would this affect future notifications? Does componentDidMount need to be initialized in render for me to send notifications in the future? Are there any ramifications in the future by doing this?
It's kinda hard to find information of this, thank you for any replies and advice.
Nope!
componentDidMount
is just a react lifecycle method and so won't effect gathering permissions.If you are using a button to ask for permissions then the page will already be mounted. If you were to want to ask for permissions automatically when a screen loads, then using
componentDidMount
is the right approach.Expo also offers methods to check whether the user has granted permissions for location and notifications. When you ask for notifications permissions be sure to save the expo notification token you get back, and if you don't successfully save it expo allows you to get it again asynchronously.