Skip to content

Instantly share code, notes, and snippets.

@fabioespinosa
Last active February 9, 2018 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabioespinosa/0385c525bfc44d2c2ee8f86fc403e05b to your computer and use it in GitHub Desktop.
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
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