Created
September 27, 2017 22:18
-
-
Save Senhordim/2b6ab421c6ef3fd5549c3cfc87d1a3ec to your computer and use it in GitHub Desktop.
Firebase exemplos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { View, Text, Button } from 'react-native'; | |
import firebase from 'firebase'; | |
export default class App extends Component { | |
constructor(props){ | |
super(props); | |
this.state = { pontuacao: '' } | |
} | |
componentWillMount() { | |
const config = { | |
apiKey: "AIzaSyCS2Cwy8dMu9Qr21rBOlHcRvpFZWydhOZc", | |
authDomain: "fir-react-9c680.firebaseapp.com", | |
databaseURL: "https://fir-react-9c680.firebaseio.com", | |
projectId: "fir-react-9c680", | |
storageBucket: "", | |
messagingSenderId: "450751060112" | |
}; | |
firebase.initializeApp(config); | |
} | |
salvarDados() { | |
const users = firebase.database().ref('users'); | |
users.push().set({ | |
name: 'Diego Collares', | |
age: 33, | |
place: 'Taubaté - SP' | |
}); | |
} | |
listarDados() { | |
const pontuacao = firebase.database().ref('pontuacao'); | |
pontuacao.on('value', (snapshot) => { | |
const pontos = snapshot.val(); | |
this.setState({ | |
pontuacao: pontos | |
}); | |
}); | |
} | |
render(){ | |
let { pontuacao } = this.state | |
return( | |
<View> | |
<Button | |
title="Salvar dados" | |
onPress={() => { this.salvarDados() }} | |
/> | |
<Button title="Listar dados" onPress={() => { this.listarDados() }} /> | |
<Text> {pontuacao} </Text> | |
</View> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment