[React Native + Firebase] React native:real Time Database with Firebase-Setup & CRUD

Source: Internet
Author: User



// v3.2.1

Config Firebase:

First we need to require Firebase:

 from ' Firebase ';

Then on the component constructor, we need to init Firebase:

Constructor (props) {super (props); //Initialize Firebase        varConfig ={apiKey:"<api_key>", Authdomain:"<authDomain>", Databaseurl:"<databaseURL>", Storagebucket:"<storageBucket>",        };    Firebase.initializeapp (config); }

The config can easily get from the your Firebase App page.

Create New Node:set ()

  Writeuserdata (userId, name, email, imageUrl) {firebase.database ().  ref  ( "  '  + userId '). set           //  calling  this . Writeuserdata (1 ,  " zhentian Wan  , "  [email protected]  , null  );

Then one node would be created in the "Users" node, and the UID is "1".

Create new Child Node:push ()

Appendonemoreuser (name, email, imageUrl) { letref= Firebase.database ().ref(). Child ('Users'). Push ({username:name, email:email});  This. Updateme ("Yoona","[email protected]",ref. Key); }//calling This. Appendonemoreuser ("Yuri",'[email protected]',NULL);

So under "Users" node, we want to append one new node, the UID would be generated randomly.

The "ref" object contains "key" prop which ref to the UID in the database.

Update one node:update ()

updateme (name, email, key) {ConstUpdate ={username:name, email}; Letref= Firebase.database ().ref('users/'+key). Update (UPDATE). Then (RES)={Console.log ("Data has been updated");    }); }//Calling:Const ref= Firsebase.database ().ref(). Child ('Users'). push (user); This. Updateme ("Yoona","[email protected]",ref. key);

Update () and set () methods both return Promise. So you can chaining. then () onto it.

Delete Node:remove ()

    deleteme () {        firebase.database (). ref ('users/1'). Remove ();    

Delete the UID = 1 user.

[React Native + Firebase] React native:real Time Database with Firebase-Setup & CRUD

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: