2021年11月14日 星期日

How to read/write the Firebase real-time database using React-Expo

How to read/write the Firebase real-time database using React-Expo

井民全, Jing, mqjing@gmail.com


This document shows you how to read/write data to the firebase real-time database using Firebase version 8. 



GitHub: Source

1. Install

yarn add firebase



2. Coding

3. Setup Realtime Database

3.1. Temporarily Bypass Default Security Rules

Disable the read/write rules, temporarily.

Step 1: Firebase Console >> Realtime Database


Step 2: Rules tab

3.2. Get the Configuration from your Firebase Project

Step 1. Click [Setup]

Step 2: Scroll down to the [Your app]


Step 3: Copy the firebaseConfig

3.3. Code for access database.

import { StatusBar } from 'expo-status-bar';

import React, { useRef, useState } from 'react'

import { Dimensions, StyleSheet, Text, ScrollView, View, Button, Alert } from 'react-native';


import { Video } from 'expo-av'


import firebase from 'firebase'

// import {initializeApp} from 'firebase/app';

// import { getDatabase, ref, set, onValue } from 'firebase/database';


// Initialize Firebase

const firebaseConfig = {

  apiKey: "xxxxxxxx",

  authDomain: "xxxxxxxx",

  databaseURL: "xxxxxxx.app",

  projectId: "xxxxxx",

  storageBucket: "xxxxxxx",

  messagingSenderId: "xxxxxx",

  appId: "xxxxxx"

};



// https://stackoverflow.com/questions/43331011/firebase-app-named-default-already-exists-app-duplicate-app

if (!firebase.apps.length) {

  Alert.alert('new initial firebase app');

  firebase.initializeApp(firebaseConfig);

} else {

  Alert.alert('if already initialized, use that one');

  firebase.app(); // if already initialized, use that one

}


// firebase.initializeApp(firebaseConfig);


function storeHighScore(userId: any, score: any) {

  // Ref: 

  // 1. https://firebase.google.com/docs/reference/node/firebase.database.Database

  // 2. https://firebase.google.com/docs/reference/js/v8/firebase.database.Database#ref


  const db = firebase.database();

  var rootRef = firebase.database().ref();

  var userRef = rootRef.child('users/' + userId);

  // var userIdRef = firebase.database().ref('users/' + userId);


  userRef.set({ highscore: score }, () => { Alert.alert('write ok.') })

  // const reference = ref(db, 'users/' + userId);

  // set(ref(db, 'users/' + userId), {

  //   highscore: score,

  // });

}


function getHighScore(userId:any) {

  // ref. https://firebase.google.com/docs/database/web/read-and-write#web-version-8

  const db = firebase.database();

  var rootRef = firebase.database().ref();

  var userRef = rootRef.child('users/' + userId);


  userRef.on('value', (snapshot) => {

    const data = snapshot.val();

    Alert.alert('read data = ' + JSON.stringify(data));

  });

}



// Alert.alert('Play and seek to 100ms')

// storeHighScore('1234', '1234');

// Alert.alert('store high score');


const { width: DEVICE_WIDTH, height: DEVICE_HEIGHT } = Dimensions.get("window");


export default function App() {

  const video = React.useRef(null as any);

  const [status, setStatus] = React.useState({} as any);


  return (

    <View style={styles.container}>

      <Text style={[styles.text, { fontWeight: 'bold', textTransform: 'uppercase' }]}>

        Examples 1

      </Text>


      <Video

        ref={video}

        style={{ width: DEVICE_WIDTH, height: 300 }}

        source={{ uri: 'http://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4' }}

        shouldPlay

        useNativeControls

        resizeMode="contain"

        isLooping

        onPlaybackStatusUpdate={status => setStatus(() => status)}

      />


      {/* Control button view */}

      <View style={styles.viewControlButton}

      >


        <Button

          title={'write'}

          onPress={() => {

            storeHighScore('testUserId', '1234');

          }

          }

        />


       <Button

          title={'read'}

          onPress={() => {

            getHighScore('testUserId');

          }

          }

        />



        {/* Play button */}

        <Button

          title={status.isPlaying ? 'Pause' : 'Play'}

          onPress={() =>

            status.isPlaying ? video.current.pauseAsync() : video.current.playAsync()

          }

        />


        {/* Seek button */}

        <Button

          title={'Seek'}

          onPress={() => {

            if (status.isPlaying) {

              video.current.setPositionAsync(2000);

            } else {

              Alert.alert('Click [Play]');

            }



          }

          }

        />

      </View>


      <StatusBar style="auto" />

    </View>


  );

}


const styles = StyleSheet.create({

  container: {

    flex: 1,

    backgroundColor: '#fff',

    alignItems: 'center',

    justifyContent: 'center',

  },


  viewControlButton: {

    flex: 0,

    flexDirection: 'row',

    justifyContent: 'center',

    alignItems: 'center',

  },


  text: {

    marginTop: 36,

    marginBottom: 12,

  },

});


4. Result




5. References

  1. https://console.firebase.google.com/

  2. https://docs.expo.dev/guides/using-firebase/

  3. https://medium.com/swlh/lets-create-mobile-app-with-react-native-and-firebase-6967a7946408

  4. https://firebase.google.com/docs/database/web/read-and-write#web-version-8

  5. Firebase, v8, https://firebase.google.com/docs/reference/js

  6. V9 supported issue, https://stackoverflow.com/questions/69236123/firebase-modular-v9-incompatible-with-expo