Run a function and if it takes longer than x seconds of error - React Native

Asked

Viewed 550 times

0

Good afternoon!

I made a function that searches the data of a JSON on the Web, but I wanted to involve it in a function that took longer that for example 10 seconds exploded an error on the screen, currently the page is only blank, only explode the error if I change the url of get

componentDidMount(){

    this.setState({loading:true})
    setTimeout(()=>{
        axios
        .get('LINK DO JSON')
        .then(response =>{
            const {dados} = response.data;
            //colocando no state
            this.setState({
                pessoas: dados,
                loading: false,

            })
        }).catch(error => {
            this.setState({
                loading: false,
                error: true,
            })      
        })
    },8000)    
}

If it takes longer than 10 seconds I wanted to give an error, but only gives the error if I move there in the JSON link and put a non-existent. setTimeOut keeps loading it for these "8 seconds - 8000 milliseconds', but then it goes blank and wanted q to explode an error instead

Code that calls later:

render() {      
    return (
        <View style = {styles.container}>       
        {
            this.state.loading 
            ? <ActivityIndicator size='large' color='#97c19a'/>
            : this.state.error
            ? <View> 
                  <Text style={styles.error}>Ops... Algo deu errado =(</Text>
                  <Text style={styles.error2}>Tente recarregar a página!</Text>
                  <Text style={styles.error2}>Se o problema persistir, contate a TI!</Text>
              </View>
            : <ListaDePessoas pessoas={this.state.pessoas} onPressItem={pageParams => {this.props.navigation.navigate('PeopleDetailPage', pageParams)}}/>
        }       
        </View>
    );
}}
  • try to use the Axios call without the setTimeout and replacing the axios.get('LINK DO JSON') for axios.get('LINK DO JSON', {timeout: 10000})

  • Until it worked, but when I put 10 seconds for example, [forcing the error] loading("Activity Indicator") is less than 10 seconds and the screen again turns gray/white, only with the sample header. This the user would not understand what happened, but when I put 1 second on the timeout, it works normally. And also to be able to get this json link the user has to be on a wifi own if he can not see the data, I do not know if this has to do

  • In your tests the request returns success or error?

  • Returns a gray page, which is the color of my background, but not the error, but also does not load any page. with the list of people to upload

  • I tested here https://snack.expo.io/B1_sK8N0X using without the api link and returned the right error as it should be, only thing I changed was the function of componentDidMount for componentWillMount.

No answers

Browser other questions tagged

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