Reactjs 如何每30分钟在瓷砖层(反应传单)中申请新瓷砖?
这就是我所拥有的:Reactjs 如何每30分钟在瓷砖层(反应传单)中申请新瓷砖?,reactjs,leaflet,mapbox,Reactjs,Leaflet,Mapbox,这就是我所拥有的: const RL = require('react-leaflet'); var Map = RL.Map; var TileLayer = RL.TileLayer; <Map zoom={14} zoomSnap={0} style={{height: "100%"}} ref={(map) => { this.map = map; }} > <TileLayer url={`https://api.mapbox.com/st
const RL = require('react-leaflet');
var Map = RL.Map;
var TileLayer = RL.TileLayer;
<Map
zoom={14}
zoomSnap={0}
style={{height: "100%"}}
ref={(map) => { this.map = map; }}
>
<TileLayer url={`https://api.mapbox.com/styles/v1/${process.env.MAPBOX_URL}/tiles/256/{z}/{x}/{y}@2x?fresh=true&access_token=${process.env.MAPBOX_API}`}/>
const RL=require('react-传单');
var-Map=RL.Map;
var TileLayer=RL.TileLayer;
{this.map=map;}}
>
但我不知道如何每30分钟申请一次新瓷砖?我需要它来显示更新的交通信息…您的目标是每隔30分钟重新渲染包含
Map
和tillelayer
的组件。为此,组件中需要某种状态,这种状态必须每30分钟更改一次。要安排更新,可以使用setInterval
方法
话虽如此,让我们假设您的组件被称为Parent
,并构建它:
class Parent extends Component {
state = {
updatesCount: 0
};
intervalId = null;
componentDidMount() {
this.intervalId = setInterval(this.update, 1800000) // every 30 mins
}
update = () => {
// this function will be called every 30 mins, updating the state and causing the component to re-render
this.setState({ updatesCount: this.state.updatesCount++ });
}
componentWillUnmount() {
clearInterval(this.intervalId);
}
render() {
<div>
<Map
zoom={14}
zoomSnap={0}
style={{height: "100%"}}
ref={(map) => { this.map = map; }}
>
<TileLayer url={`https://api.mapbox.com/styles/v1/${process.env.MAPBOX_URL}/tiles/256/{z}/{x}/{y}@2x?fresh=true&access_token=${process.env.MAPBOX_API}`}/>
</div>
}
}
类父级扩展组件{
状态={
更新计数:0
};
intervalId=null;
componentDidMount(){
this.intervalId=setInterval(this.update,1800000)//每30分钟
}
更新=()=>{
//此函数将每30分钟调用一次,更新状态并使组件重新渲染
this.setState({updateCount:this.state.updateCount++});
}
组件将卸载(){
clearInterval(此为有效期);
}
render(){
{this.map=map;}}
>
}
}
您的目标是每隔30分钟重新渲染包含Map
和TileLayer
的组件。为此,组件中需要某种状态,这种状态必须每30分钟更改一次。要安排更新,可以使用setInterval
方法
话虽如此,让我们假设您的组件被称为Parent
,并构建它:
class Parent extends Component {
state = {
updatesCount: 0
};
intervalId = null;
componentDidMount() {
this.intervalId = setInterval(this.update, 1800000) // every 30 mins
}
update = () => {
// this function will be called every 30 mins, updating the state and causing the component to re-render
this.setState({ updatesCount: this.state.updatesCount++ });
}
componentWillUnmount() {
clearInterval(this.intervalId);
}
render() {
<div>
<Map
zoom={14}
zoomSnap={0}
style={{height: "100%"}}
ref={(map) => { this.map = map; }}
>
<TileLayer url={`https://api.mapbox.com/styles/v1/${process.env.MAPBOX_URL}/tiles/256/{z}/{x}/{y}@2x?fresh=true&access_token=${process.env.MAPBOX_API}`}/>
</div>
}
}
类父级扩展组件{
状态={
更新计数:0
};
intervalId=null;
componentDidMount(){
this.intervalId=setInterval(this.update,1800000)//每30分钟
}
更新=()=>{
//此函数将每30分钟调用一次,更新状态并使组件重新渲染
this.setState({updateCount:this.state.updateCount++});
}
组件将卸载(){
clearInterval(此为有效期);
}
render(){
{this.map=map;}}
>
}
}