[英]How to set a state to refresh a google-map-content automatically
I built a small boat visualizer.我构建了一个小船可视化器。 I am using AISHub APIs .
我正在使用AISHub API 。 After fetching data from the APIs I am able to obtain a
json
file with the vessels I am interested in and inject these vessels inside a table.从 API 获取数据后,我能够获取包含我感兴趣的容器的
json
文件,并将这些容器注入表中。 The user has to manually update the page pushing the refresh button on top left of the page to see the new updated table.用户必须手动更新页面,按下页面左上角的刷新按钮才能看到新的更新表。
The problem : How to set a state to refresh the google-map
content automatically every minute instead of the user doing it manually?问题:如何设置状态以每分钟自动刷新
google-map
内容,而不是用户手动执行?
Below the code:代码下方:
GoogleMap.js谷歌地图.js
class BoatMap extends Component {
constructor(props) {
super(props);
this.state = {
buttonEnabled: true,
buttonClickedAt: null,
progress: 0,
ships: [],
type: 'All',
shipTypes: [],
activeShipTypes: [],
logoMap: {}
};
this.updateRequest = this.updateRequest.bind(this);
this.countDownInterval = null;
}
async componentDidMount() {
this.countDownInterval = setInterval(() => {
if (!this.state.buttonClickedAt) return;
const date = new Date();
const diff = Math.floor((date.getTime() - this.state.buttonClickedAt.getTime()) / 1000);
if (diff < 90) {
this.setState({
progress: diff,
buttonEnabled: false
});
} else {
this.setState({
progress: 0,
buttonClickedAt: null,
buttonEnabled: true
});
}
}, 500);
await this.updateRequest();
const shipTypeResults = await Client.getEntries({
content_type: 'competitors'
});
console.log(shipTypeResults);
const shipTypes = shipTypeResults.items.map((data) => data.fields);
const logoMap = shipTypes.reduce((acc, type) => {
return {
...acc,
[type.name]: type.images.fields.file.url
};
}, {});
console.log({ shipTypes });
this.setState({
logoMap
});
}
componentDidUpdate(prevProps, prevState) {
if (this.state.type !== prevState.type) {
}
}
componentWillUnmount() {
clearInterval(this.countdownInterval);
}
async updateRequest() {
const url = 'http://localhost:3001/hello';
console.log(url);
const fetchingData = await fetch(url);
const ships = await fetchingData.json();
console.log(ships);
this.setState({
buttonEnabled: false,
buttonClickedAt: new Date(),
progress: 0,
ships
});
setTimeout(() => {
this.setState({ buttonEnabled: true });
});
}
render() {
return (
<div className="google-map">
<GoogleMapReact
bootstrapURLKeys={{ key: 'KEY' }}
center={{
lat: this.props.activeShip ? this.props.activeShip.latitude : 42.4,
lng: this.props.activeShip ? this.props.activeShip.longitude : -71.1
}}
zoom={8}
>
</GoogleMapReact>
</div>
);
}
}
What I have done so far:到目前为止我做了什么:
A good way would be using a setTimeout()
but would that be correct?一个好方法是使用
setTimeout()
但这是否正确? Where should that be applied and how?应该在哪里应用以及如何应用?
setTimeout(function () {
location.reload();
}, 60 * 1000);
Or maybe setting an interval as a refresh rate?或者可能将间隔设置为刷新率?
I am a bit confused on what would the best way to approach this.我对解决此问题的最佳方法感到有些困惑。
On your request function i guess u want to disable the button while the api doesn't return, so maybe move this piece above the requests:在您的请求功能上,我想您想在 api 不返回时禁用该按钮,因此可以将这部分移到请求上方:
this.setState({
buttonEnabled: false,
buttonClickedAt: new Date(),
progress: 0,
ships
});
If im wrong you could remove the timeout from the second setState and call as a callback on the first like this:如果我错了,你可以从第二个 setState 中删除超时,并像这样调用第一个回调:
this.setState({
buttonEnabled: false,
buttonClickedAt: new Date(),
progress: 0,
ships
}, () => {
this.setState({ buttonEnabled: true });
});
on the last part instead of location.reload() set a interval calling the update on ur componentDidMount:在最后一部分而不是 location.reload() 设置一个间隔调用你的 componentDidMount 上的更新:
let updateInterval = setInterval(() => {
this.updateRequest();
}, 60 * 1000);
this.setState({updateInterval})
then on the componentWillUnmount you clear the interval this.state.updateInterval
然后在 componentWillUnmount 上清除间隔
this.state.updateInterval
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.