Reading of Firebase data

Asked

Viewed 553 times

0

Whoa, that’s all right?

I’m doing a college project using the Firebase and the React Native.

I developed a screen where I want to publish texts and show everyone who is using the application. I was able to save the data in the database, but I can’t show it on the screen.

This is called to read the database data:

listarPub() {
    const publicadas = firebase.database().ref('Publicacoes').child('publicacao');
    publicadas.on('value', (snapshot) => {

   this.setState({ publicadas: snapshot.val()});
  });

}

However, I have no idea how to apply in a Text and/or FlatList for example.

I wonder if this flame is correct and how do I show it on the screen. I tried several topics, videos, etc, but I could not get anything to help me.

NOTE: It’s my first post here, so I don’t know how to express myself right is my first experience with React and Firebase.

Thank you!

1 answer

0

export default class ProductSpotLight extends Component {
    constructor() {
        super()
        this.state = {
            publicadas: null
        }
    }
    componentDidMount() {
        if (!this.state.publicadas) {
            const publicadas = firebase.database().ref('Publicacoes').child('publicacao');
            publicadas.on('value', (snapshot) => {
                this.setState({ publicadas: snapshot.val() });
            })
        }
    }
    render() {
        const { publicadas } = this.state

        return (
            <View>
                {
                    publicadas &&
                    <Text>{publicadas.texto}</Text> //AQUI VAI CORRESPONDER AO VALOR DA PROPRIEDADE DO OBJETO QUE VOCE DESEJA MOSTRAR
                }
            </View >
        )
    }
}
  • Hi Murilo. So, I did this way and still not returned. I created a console log: console.log(published.on('value', (snapshot) => { this.setState({ published: snapshot.val() }); and returns as [Function Anonymous]. Can you help me with that?

  • can add error log from red screen?

  • It does not error, but does not return the database posts. There is something I can do to clarify better the reason for not returning the data?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.