I
I
Ilya2019-03-20 21:10:55
React Native
Ilya, 2019-03-20 21:10:55

How to make automatic page refresh?

I have a question.
My function takes data from firebase. This date is updated periodically.
Data parses elements onto the screen (data.map).
How can I make it so that every time I visit the page, the data is updated?
Now the function is called via componentDidMount.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pcdesign, 2019-03-20
@pcdesign

Try componentWillMount()

import React, { Component } from 'react';
import { View, Text } from 'react-native';

export default class App extends React.Component  {

    constructor(props) {
      super(props);

      this.state = {
        data : null
      };
    }

    componentWillMount() {
        this.renderMyData();
    }

    renderMyData(){
        fetch('https://your url')
            .then((response) => response.json())
            .then((responseJson) => {
              this.setState({ data : responseJson })
            })
            .catch((error) => {
              console.error(error);
            });
    }

    render(){
        return(
            <View>
                {this.state.data ? <MyComponent data={this.state.data} /> : <MyLoadingComponnents /> }
            </View>
        );
    }
}

https://stackoverflow.com/questions/30929679/react...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question