Guest User

Untitled

a guest
Jan 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. import React from 'react';
  2. import {
  3. StyleSheet,
  4. Image,
  5. Text,
  6. View
  7. } from 'react-native';
  8.  
  9. import firebase from 'react-native-firebase';
  10.  
  11. export default class StateScreen extends React.Component {
  12. constructor() {
  13. super();
  14.  
  15. this.state = {
  16. name: '',
  17. };
  18.  
  19. this.stateRef = firebase.firestore().collection('states').orderBy('added_at', 'desc').limit(1);
  20. this.unsubscribe = null;
  21. }
  22.  
  23. onCollectionUpdate = (querySnapshot) => {
  24. let state;
  25. // limited to one up above
  26. querySnapshot.forEach(doc => state = doc.data().name);
  27. this.setState({name: state});
  28. }
  29.  
  30. componentDidMount() {
  31. this.unsubscribe = this.stateRef.onSnapshot(this.onCollectionUpdate);
  32. }
  33.  
  34. componentWillUnmount() {
  35. this.unsubscribe();
  36. }
  37.  
  38. render() {
  39. return (
  40. <View style={styles.container}>
  41. <Image source={require('../assets/confused-emoji.png')} style={styles.logo} />
  42. <Text style={styles.header}>
  43. What is George's state?
  44. </Text>
  45. <Text style={styles.stateName}>
  46. {this.state.name}
  47. </Text>
  48. </View>
  49. );
  50. }
  51. }
  52. const styles = StyleSheet.create({
  53. container: {
  54. flex: 1,
  55. alignItems: 'center',
  56. backgroundColor: '#F5FCFF',
  57. paddingTop: 50,
  58. },
  59. logo: {
  60. height: 120,
  61. width: 120,
  62. marginBottom: 16,
  63. },
  64. header: {
  65. fontSize: 30,
  66. textAlign: 'center',
  67. margin: 10,
  68. },
  69. stateName: {
  70. textAlign: 'center',
  71. color: '#333333',
  72. marginBottom: 5,
  73. fontSize: 40,
  74. },
  75. });
Add Comment
Please, Sign In to add comment