How can a react-native app save a variable after the app is turned off? - react-native

I want to make an app remember some data that a user puts in even after he turns off the phone (like auto-login). How can this be done in React Native? Or which libraries/components should I research?

If you want to do something like auto-login you should probably be using react-native-keychain so that you can store user credentials in the keychain on the device in a more secure way.
If you are looking to store a particular application state or other non sensitive data you could use react-native-simple-store which provides a really simple wrapper around AsyncStorage and takes care of the boilerplate JSON.stringify/JSON.parse that you need to do for objects and arrays.
Hope that helps!

you can use flux stores, async store to store the current state in store, and re-route based on the store
can use componentWillMount mixing for setting up state before rendering view. for e.g.:
componentWillMount() {
LocalStorage.bootstrap(() => this.setState({bootstrapped: true}));
},
you can refer https://github.com/brentvatne/flux-util/blob/master/lib/flux-util.js for example.

You can use AsyncStorage bundled with react-native. It creates key value pairs
AsyncStorage.setItem("key", "value").done() // both key and value have to be strings
AsyncStorage.getItem("key").then((value) =>{}).done()
You can check the documentation for more options
https://facebook.github.io/react-native/docs/asyncstorage.html#content

Related

Best Way To Globally Instantiate Ably Connection in React Native

I am building a React Native game with multiple screens. I am trying to instantiate Ably one time and use it throughout the application without having to call new Ably('api-key') on every screen, as this is creating multiple new connections and making it impossible to keep a single connectionId. Is there a way to architect this so that I can define
const client = new Ably('api-key')
one time in App.js and use it throughout the other screens? Either by passing it as props through React Navigation props or using Context? The documentation on this is sparse as most of their documentation only uses this on one page.
Redux will not work. You can create an Ably client and pass it to other components using props, or just use React context to make it available to the whole application. Using redux to store an Ably client will cause an error since Ably clients aren’t serialisable.
See https://reactjs.org/docs/context.html for more info.
Here are some examples:
https://github.com/ably-labs/react-hooks
https://ably.com/blog/ably-react-hooks-npm-package
Cheers,
Richie

Would FirebaseAuth return my current user for Widget and IntentExtension while having the main project as React native?

I am currently creating a WidgetKit for my React Native application. However, I need to create a post request to retrieve my current logined user information. I was wondering if importing FirebaseAuth in my WidgetKit would return the correct current user in my widget project?
import FirebaseAuth
func getUser(){
FIRAuth.auth().currentUser?.uid
}
I wouldn't recommend requiring actual Firebase functionality in a WidgetExtension unless your Widget requires data that may change. If it really does, you should just be including your Firebase code in the build for the Widget, you don't want to duplicate the code you've written for interacting with Firebase.
If you don't need live data, just save the Object your widget needs access to into the UserDefaults. You can do this easily so long as the object in question conforms to Codable. I'm assuming you're suggesting using FirebaseAuth because the Object you want to construct relies on your Firebase data in some way, but there's no need to actually interact with Firebase in a Widget. The log in status of your user can't change if they aren't in your app, so it's not particularly unsafe to save the data directly. If they sign out you can just clear the UserDefaults value.

Is there a way to attach console.log to firebase analytics event?

I am setting up firebase analytics for my React Native app. Is it possible to attach a console.log that fires each time firebase.analytics().logEvent() is called? I want to be able to see the event name and params in the console.log. I know I can check in the DebugView in Firebase, but just having it logged to the console seems a bit faster. Appreciate any input.
Per the comment, it would be more economical to create a global method that sends the data to Analytics and console together.
You can read about Global Helpers HERE.
module.exports = function(payload) {
firebase.analytics().logEvent()
console.log(payload);
}
_.Log(payload);
This allows you to also introduce filters and edge-case scenarios within your app as needed including debug mode and alerts from within your app.

How to start Redux in React Native

I am new to React Native and I want to add Redux to my project.
And I want to know if redux store values are saved when app is exited.
If it is not saved, how can I handle this problem?
In web development, we use cookie and local storage. Thanks in advance.
[Edit]
If I must use async storage, is there any easy way to approach this?
Redux state is usually emptied upon when an app is exited (not minimised).
What you should to instead is to use AsyncStorage to persist your access token, and get them from same storage when the app starts. You can do this in a didMount lifecycle, or any other means you may choose.
There is Ayncstorage for react native which is equivalent to localstorage in browser.
You can refer this
https://reactnative.dev/docs/asyncstorage
Or you can use redux-persist if you want to persist redux store when app exits which would otherwise get lost.
https://github.com/rt2zz/redux-persist

Geofencing in React Native

At the time of this post I believe that there are no geofencing modules available for react native so I would like to implement an alternative poor man's strategy. I discovered react native's geolocation module however the official doc is not clear:
1) Does the Geolocation module run in the background and get the current user coordinates automatically (even if the app is in the background)? If yes, are these stored in a variable or a state?
2) If (1) is true, how can I detect a change in state? Because once I detect a change in state (i.e. user's location) I would like to push this new location to a remote server and store it in a database. On the other hand I do not want to store each and every inch the user is moving!
Does this strategy make sense at all? My concerns are battery consumption as well of course..
1) You need to enable this feature in xcode Then save the location you get from the 'navigator.geolocation.getCurrentPosition' callback somewhere in you app e.g. in your AsyncStorage
2) The callback is only getting called if the location is changed. So you don't need explicitly detect any location change.