[英]How to update state?
I am trying to update state with API. 我正在尝试使用API更新状态。
I tried to update API state with using setouttime, 我尝试使用setouttime更新API状态,
in render(), there is if statement which if timepassed is true, this.get_bus will be called to update state. 在render()中,有if语句,如果timepassed为true,则将调用this.get_bus来更新状态。 However I got warning cannot update during an existing state transition
但是我得到警告,在现有状态转换期间无法更新
Please let me know how to update... 请让我知道如何更新...
this is my code. 这是我的代码。
export default class App extends React.Component {
constructor(){
super()
this.state = {
station1: [],
station2: [] ,
timePassed:false,
}
}
get_BUS(code,station){
return fetch('https://transportapi.com/v3/uk/bus/stop/'+code+'/live.json?
app_id=6ab4b5a0&app_key=7693c12908e7d566351e3610b4acfa9f
&group=route&nextbuses=yes')
.then((response) => response.json())
.then((responseJson) => {
for(var x in responseJson.departures){
this.setState({
state : this.state[station].push(responseJson.departures[x][0]["line"],":
",responseJson.departures[x][0]["aimed_departure_time"]," ")
});
}
})
.catch((error) => {
console.error(error);
});
}
componentWillMount(){
this.get_BUS('01000053207','station1')
this.get_BUS('01000053207','station2')
this.setTimePassed(false)
}
setTimePassed(abc) {
this.setState({timePassed: abc});
}
render() {
.....
let that = this;
setTimeout(function(){
that.setState({timePassed: true})}, 10000);
if(this.state.timePassed){
console.log("HI update")
this.get_BUS('01000053207','station1')
this.get_BUS('01000053207','station2')
this.setTimePassed(false)
}
return (
......
) )
Here's why you would get that error: When you update state, you use 这就是为什么您会收到该错误的原因:更新状态时,您使用
this.setState({ ... });
However, this.setState({ ... })
method will call render()
method when the state has been updated. 但是,状态更新后,
this.setState({ ... })
方法将调用render()
方法。 Therefore, you're repeatedly calling render()
as you update states in it before the return
block. 因此,您在更新
return
render()
块之前的状态时反复调用render()
。
If I get it right, you want to call get_bus()
every 10 seconds. 如果我做对了,您想每10秒调用
get_bus()
。 Look at the following example first, in this example, I have state: 首先看下面的例子,在这个例子中,我有状态:
this.state = {
timer: null,
counter: 0
};
Where timer
is a timer, it will call the function we want to execute every time some seconds have passed, and we use counter
to count how many seconds have passed, look at the code below: 在
timer
是计时器的地方,每隔几秒钟就会调用我们要执行的函数,我们使用counter
来计算经过多少秒,请看下面的代码:
import React, { Component } from 'react';
import { View, Text } from 'react-native';
class App extends Component {
constructor(props) {
super(props);
this.state = {
timer: null,
counter: 0
};
this.tick = this.tick.bind(this);
};
// Set up our timer here
componentDidMount() {
let timer = setInterval(this.tick, 1000);
this.setState({ timer });
}
// Add 1 to counter for every second passed
tick() {
this.setState({ counter: this.state.counter + 1 });
}
render() {
return (
<View style={{ ... }}>
<Text style={{ ... }}>
{this.state.counter}
</Text>
</View>
);
}
}
export default App;
As you can see, in componentDidMount()
, we use setInterval()
method to set up our timer. 如您所见,在
componentDidMount()
,我们使用setInterval()
方法来设置计时器。 setInterval()
methods takes 2 arguments, the first argument is the function you wish to execute, the second argument is the interval in milliseconds, this is what the app looks like: setInterval()
方法有2个参数,第一个参数是您要执行的函数,第二个参数是间隔(以毫秒为单位),这是应用程序的外观:
In your case, if you wish to call get_bus()
every 10 seconds, simple change the two arguments to this.get_bus(...)
and 10000
对于您的情况,如果希望每10秒调用
get_bus()
, get_bus()
将两个参数分别更改为this.get_bus(...)
和10000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.