javascript - 如何自动引用 ReactJS 应用程序
问题描述
我建立了一个小船可视化器。我正在使用AISHub API。从 API 获取数据后,我可以获得一个json
包含我感兴趣的容器的文件,并将这些容器注入一个表中。API 允许在 1 分钟后继续进行新的提取,如其文档中所述。用户必须手动更新页面,按下页面左上角的刷新按钮才能看到新的更新表。
问题:是否可以在用户不手动刷新页面的情况下每分钟进行一次自动更新?
为了绕过一分钟的问题,我必须组织一个缓存过程,实际上效果很好。我无需等待一分钟即可刷新,但我必须手动完成。
index.js
var express = require('express');
var router = express.Router();
var axios = require('axios');
const NodeCache = require('node-cache');
const myCache = new NodeCache();
let hitCount = 0;
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/hello', async function(req, res, next) {
const allData = myCache.get('allData');
if (!allData) {
hitCount++;
console.log(`hit ${hitCount} number of times`);
const { data } = await axios.get(
'http://data.aishub.net/ws.php?username=KEY&format=1&output=json&compress=0&latmin=11.42&latmax=58.20&lonmin=-134.09&lonmax=-52.62'
);
console.log(data + 'ERR');
const [ metaData, ships ] = data;
const shipsOfInterest = ships.filter(
(ship) =>
mmsiOfInterest.includes(ship.MMSI) ||
shipNamesOfInterest.includes(ship.NAME) ||
imoOfInterest.includes(ship.IMO)
);
myCache.set('allData', shipsOfInterest, 70);
console.log(shipsOfInterest);
res.send(shipsOfInterest);
return;
}
console.log('this is the data:', allData);
res.send(allData);
});
module.exports = router;
谷歌地图.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>
);
}
}
我一直在研究如何在不让用户手动操作的情况下自动刷新页面。感谢您指出解决此问题的正确方向。
解决方案
不确定我是否完全理解这个问题。什么是测绘平台?理想情况下,您根本不会刷新页面。您将使用 state 每 60 秒通过一个新的 ajax 调用刷新地图内容。
如果您只想每 60 秒重新加载一次,那么下面的方法将起作用。
setTimeout(function () {
location.reload();
}, 60 * 1000);
推荐阅读
- javascript - React 如何将 HTML 标签存储为 Javascript 对象
- c - 如何在 C 中修复此“']' 之前的预期表达式”错误消息?
- python - 用于多维数组的 Python 快速数组乘法
- scala - 在scala中转换SAS日期
- css - 如何在较大设备上的图像上设置最大宽度而不破坏 Retina 屏幕上的外观?
- python - 属性错误:“int”对象没有属性“get”
- python - django 在创建帖子后将用户重定向到帖子
- python - Google 错误 - 尝试使用 Python selenium 登录时无法登录
- angular - 在另一个服务中未定义角度翻译服务
- r - dplyr:如何根据其他列中的值计算组内的折叠变化