Last active
February 9, 2018 03:45
-
-
Save fabioespinosa/0385c525bfc44d2c2ee8f86fc403e05b to your computer and use it in GitHub Desktop.
This is a simple group chat app built with React Native that uses Firebase and Native Base
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 { | |
Container, | |
Header, | |
Content, | |
List, | |
ListItem, | |
Left, | |
Body, | |
Right, | |
Thumbnail, | |
Text, | |
Input, | |
Button | |
} from 'native-base'; | |
import firebase from 'firebase'; | |
export default class ListAvatarExample extends Component { | |
state = { | |
texto: '', | |
mensajes: [] | |
}; | |
componentDidMount() { | |
const config = { | |
apiKey: '', | |
authDomain: '', | |
databaseURL: '', | |
projectId: '', | |
storageBucket: '', | |
messagingSenderId: '' | |
}; | |
firebase.initializeApp(config); | |
firebase | |
.database() | |
.ref('/mensajes') | |
.on('value', snapshot => { | |
const mensajes = []; | |
Object.keys(snapshot.val()).forEach(key => { | |
mensajes.push(snapshot.val()[key]); | |
}); | |
console.log(mensajes); | |
this.setState({ mensajes: mensajes.reverse() }); | |
}); | |
} | |
enviar = () => { | |
const obj = {}; | |
this.state.mensajes.forEach((mensaje, index) => { | |
obj[index] = mensaje; | |
}); | |
obj['nuevo'] = this.state.texto; | |
firebase | |
.database() | |
.ref('/mensajes') | |
.set(obj); | |
}; | |
render() { | |
return ( | |
<Container> | |
<Header /> | |
<Content> | |
<Input | |
onChangeText={texto => { | |
this.setState({ texto }); | |
}} | |
value={this.state.texto} | |
/> | |
<Button onPress={this.enviar}> | |
<Text>Enviar</Text> | |
</Button> | |
<List> | |
{this.state.mensajes.map(mensaje => { | |
return ( | |
<ListItem avatar> | |
<Left>{/* <Thumbnail source={{ uri: 'Image URL' }} /> */}</Left> | |
<Body> | |
{/* <Text>Kumar Pratik</Text> */} | |
<Text note>{mensaje}</Text> | |
</Body> | |
<Right>{/* <Text note>3:43 pm</Text> */}</Right> | |
</ListItem> | |
); | |
})} | |
</List> | |
</Content> | |
</Container> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment