Update data automatically in react-native app

0
Hello, I'm a beginner for react-native, how do I update fields automatically when your API data changes (this change is not done in the app, it just monitors), for example: if there my database I change the name of a product in the react app automatically reflect this change, there is a component that facilitates this just wanted a starting point. Thankful. Here is the code I am doing according to the biblioca:

export default class App extends React.Component {

  constructor(props){
    super(props);
    this.state ={ 
      isLoading: true,
      _dataSource:[],
    }
  }

  componentDidMount(){
    return fetch('url minha api')
      .then((response) => response.json())
      .then((responseJson) => {

        this.setState({
          isLoading: false,
          _dataSource: responseJson.minhasinformacoes,
          get dataSource() {
            return this._dataSource;
          },
          set dataSource(value) {
            this._dataSource = value;
          },
        }, function(){

        });

      })
      .catch((error) =>{
        console.error(error);
      });
  }



  render(){

    setTimeout(() => {
      this.setState({
        _dataSource: this.state._dataSource + _dataSource
      })
    }, 1000);
    

    if(this.state.isLoading){
      return(
        <View style={{flex: 1, padding: 20}}>
          <ActivityIndicator/>
        </View>
      )
    }

    return(
      <View style={{flex: 1, paddingTop:20}}>
        /*Quando houvesse mudanças atualizasse os campos ProductName e 
          CategoryName atuamaticamente*/
        <FlatList
          data={this.state._dataSource}
          renderItem={({item}) => <Text>{item.ProductName}, {item.CategoryName} </Text>}
          keyExtractor={({id}, index) => id}
        />
      </View>
    );
  }
}

bold text

    
asked by anonymous 22.09.2018 / 19:00

1 answer

0

As good as you use API to provide data, there is no need to update in your app. With each new request in your API the data will be "updated."

Working with APIs and Requisitions: link

    
26.09.2018 / 22:39