[React Native + Firebase] React Native: Real time database with Firebase -- setup & CRUD

Install:

npm i --save firebase // v3.2.1

Config Firebase:

First we need to require Firebase:

import firebase from ‘firebase‘;

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

    constructor(props){
        super(props);
        // Initialize Firebase
        var config = {
            apiKey: "<api_key>",
            authDomain: "<authDomain>",
            databaseURL: "<databaseURL>",
            storageBucket: "<storageBucket>",
        };
        firebase.initializeApp(config);
    }

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

Create new node: set()

    writeUserData(userId, name, email, imageUrl) {
        firebase.database().ref(‘users/‘ + userId).set({
            username: name,
            email: email,
            profile_picture : imageUrl
        });
    }

// calling
    this.writeUserData(1, "Zhentian Wan", ‘[email protected]‘, null);

Then one node will be created in the "users" node, uid is "1".

Create new Child node: push()

    appendOneMoreUser(name, email, imageUrl){
        let ref = 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 will 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){
        const update = {
            username: name,
            email
        };
        let ref = 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.

时间: 2024-08-08 22:01:22

[React Native + Firebase] React Native: Real time database with Firebase -- setup & CRUD的相关文章

Native VS React Native VS 微信小程序

随着React Native和 微信小程序的出现,Native一家独大的局面出现裂痕,很多小公司使用已经正在着手微信小程序和React Native了,我公司就已经走上React Native之路.那么,React Native 和 Native 和 微信小程序 三者比较起来到底哪者更强呢?今天从适应性(适合什么项目).开发限制.软件迭代更新.成本角度谈谈自己对三者的看法. 个人角度: 适应性(适应什么项目) 大项目.游戏项目:Native 中大型项目:react native 小项目:微信小程

react native js 与 native 的通信与交互方式

JS 的启动过程 React Native 的 iOS 端代码是直接从 Xcode IDE 里启动的.在启动时,首先要对代码进行编译,不出意外,在编译后会弹出一个命令行窗口,这个窗口就是通过 Node.js 启动的 development server . 问题是这个命令行是怎么启动起来的呢?实际上,Xcode 在 Build Phase 的最后一个阶段对此做了配置,其实就是增加了一个 sh 脚本,让小葱的在编译会自动去执行这个脚本,打开 npm,相当于你直接手动命令行切到 react-nati

React Native之React速学教程(下)

概述 本篇为<React Native之React速学教程>的最后一篇.本篇将带着大家一起认识ES6,学习在开发中常用的一些ES6的新特性,以及ES6与ES5的区别,解决大家在学习React /React Native过程中对于ES6与ES5的一些困惑. ES6的特性 何为ES6? ES6全称ECMAScript 6.0,ES6于2015年6月17日发布,ECMAScript是ECMA制定的标准化脚本语言.目前JavaScript使用的ECMAScript版本为ECMAScript-262.

React Native之React速学教程(上)

概述 本篇为<React Native之React速学教程>的第一篇.本篇将从React的特点.如何使用React.JSX语法.组件(Component)以及组件的属性,状态等方面进行讲解. What's React React是一个用于组建用户界面的JavaScript库,让你以更简单的方式来创建交互式用户界面. 当数据改变时,React将高效的更新和渲染需要更新的组件.声明性视图使你的代码更可预测,更容易调试. 构建封装管理自己的状态的组件,然后将它们组装成复杂的用户界面.由于组件逻辑是用

React Native之React速学教程(中)

概述 本篇为<React Native之React速学教程>的第一篇.本篇将从React的特点.如何使用React.JSX语法.组件(Component)以及组件的属性,状态等方面进行讲解. What's React React是一个用于组建用户界面的JavaScript库,让你以更简单的方式来创建交互式用户界面. 当数据改变时,React将高效的更新和渲染需要更新的组件.声明性视图使你的代码更可预测,更容易调试. 构建封装管理自己的状态的组件,然后将它们组装成复杂的用户界面.由于组件逻辑是用

React Native与React的关系及特点

一.React.React.js和React Native的关系 React是基础框架,是一套基础设计实现理念,开发者不能直接使用它来开发移动应用或网页. 在React之上发展出了React.js框架用来开发网页,发展出来React Native用来开发移动应用. 因为React基础框架与React.js框架是同时出现.同时进化发展的,就造成了React基础框架的基本概念.设计思想都是在React.js的相关文档中描述的. 后来,Facebook推出React Native后,也没有把React

[React] Recompose: Theme React Components Live with Context

SASS Bootstrap allows us to configure theme or branding variables that affect all components (e.g. Primary Color or Link Color). When we isolate our styles inside React components we lose the ability to theme them. To get round this we can put our th

【玩转React】关于React你需要知道的事儿

前言 随着前端技术的迅猛发展,各种前端框架也随势崛起,但归根结底,支撑每一款web框架流行的强大因素都是它能更好地服务于业务. React 自然也不例外,它的开发者当初正在开发Facebook的一个广告系统,由于不满足于当下任何的 MVC 框架,所以就自己写了一套 UI 框架,于是乎大名鼎鼎的 React 就由此诞生了. React 的出现无疑为 web 开发带来了颠覆性的改变,多少开发者夜以继日只为体验一把 React 带来的快感.本文就将带领大家一起领略一番 React 的理念.特色与情怀.

[React] Style a React component with styled-components

In this lesson, we remove the mapping between a React component and the styles applied to it via classnames. We write our styles directly within the component, in a real CSS syntax, with the full power of JavaScript. Old: import React, { Component }

创建React工程:React工程模板

这是本人初学React做的学习笔记;讲的不是很深,只算是简单的进行介绍. 这是一个小系列.都是在同一个模板中搭建的,但是代码是不能正常执行的. >>index.js <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>React工程模板</title> <!-- react.js 是Reac