ahmetbcakici
ahmetbcakici

Reputation: 423

React Native get dynamic data

there is a data which is changing every second in my api and i should get it dynamically to react native screen and im using setInterval I writed a function which is send request and get data every second and i should refresh this function and i did use setInterval but im not sure about that is this healthy? also i could not solve this because i got an error that :

Possible Unhandled Promise Rejection (id: 7):
    TypeError: _this2.setState is not a function. (In '_this2.setState({
        kalanzaman: res.data
    })', '_this2.setState' is undefined)

My func and how am i calling it :

  dynamically() {
    // console.log("bom")
    axios.get(`http://localhost:5000/kalanzaman`)
      .then(res => {
        this.setState({ kalanzaman: res.data })
      })
  }

.

setInterval(this.dynamically, 1000)

Which way should i use for get dynamic data to react native from my api?

Upvotes: 0

Views: 259

Answers (2)

zidniryi
zidniryi

Reputation: 1341

Maybe it would be better if you write it like this:

setInterval(() => {
  dynamically() {
    // console.log("bom")
    axios.get(`http://localhost:5000/kalanzaman`)
      .then(res => {
        this.setState({ kalanzaman: res.data })
      }).catch((error) => {
        alert('Something Error')
     })
  } 
}, 100);

You must cath() for catching the error, and put it on componentDidMount

Upvotes: 0

gmaziashvili
gmaziashvili

Reputation: 250

Instead of Http request, you can use Websocket. It is a two-way interactive communication session between the user's browser and a server. for example: have a look at Socket.IO

Upvotes: 1

Related Questions