我想在启动状态更改反应之前触发一个延迟。希望这将使我的副牌gl动画。
当前,它会重新加载页面。
我设置了超时和时间间隔,但均未按预期工作。我想我缺少与窗口方法如何工作有关的状态。
这是我的职责
yearOnChange = e => {
const data = {
0: jan95,
1: jan00,
2: jan05
};
window.setTimeout(
this.setState({
location: data[e.currentTarget.value],
year: e.currentTarget.value
}),
10000
);
};
通过将功能传递给子组件然后通过按钮的
onClick
触发该功能,可以激活该功能。我预计会发生延迟,然后状态会发生变化。
我不假装不知道这是正确的代码,最佳实践还是正确的方法。这对我来说是一个新领域,正在寻找最佳实践/解决方案。
如果需要,可以编辑代码。
更新:
添加了整个代码以供参考。
至于何时调用它,只需将其传递给按钮,然后由该按钮触发onClick事件。
import React, { Component } from "react";
import { StaticMap } from "react-map-gl";
import { H3HexagonLayer } from "@deck.gl/geo-layers";
// import { HexagonLayer } from "@deck.gl/aggregation-layers";
import DeckGL from "@deck.gl/react";
import { jan95, jan00, jan05 } from "../../dummyData/concatData";
// import testData from "../../dummyData/testDataHexNo3.json";
import YearSelector from "../YearSelector/YearSelector";
const MAPBOX_TOKEN =
"pk.eyJ1Ijoiam5hbGV4YW5kZXIxMCIsImEiOiJjaWlobnE4eGswMDFld2RtNmZxMDl3Nzc3In0.UTaIFjrs21qB1npSeliZbQ";
const mapStyle =
"mapbox://styles/jnalexander10/cj0xo73a300rr2rta4ny2bj0d/draft/";
const INITIAL_VIEW_STATE = {
longitude: 0.1278,
latitude: 51.5074,
zoom: 7,
minZoom: 5,
maxZoom: 10,
pitch: 50,
bearing: -27.396674584323023
};
const elevationScale = { min: 0, max: 10 };
class FirstMap extends Component {
constructor(props) {
super(props);
this.state = {
location: "",
year: 0,
elevationScale: elevationScale.min
};
this.startAnimationTimer = null;
this.intervalTimer = null;
this._startAnimate = this._startAnimate.bind(this);
this._animateHeight = this._animateHeight.bind(this);
}
componentDidMount() {
this.setState({
location: jan95
});
this._animate();
}
_animate() {
this._stopAnimate();
// wait 1.5 secs to start animation so that all data are loaded
this.startAnimationTimer = window.setTimeout(this._startAnimate, 3500);
}
_startAnimate() {
this.intervalTimer = window.setInterval(this._animateHeight, 20);
}
_stopAnimate() {
window.clearTimeout(this.startAnimationTimer);
window.clearTimeout(this.intervalTimer);
}
_animateHeight() {
if (this.state.elevationScale === elevationScale.max) {
this._stopAnimate();
} else {
this.setState({ elevationScale: this.state.elevationScale + 1 });
}
}
_layerRendering = () => {
const stateLocation = this.state.location;
const valuesOfState = stateLocation[0];
return [
new H3HexagonLayer({
id: "h3-hexagon-layer",
data: valuesOfState && Object.values(valuesOfState),
pickable: true,
opacity: 0.15,
wireframe: true,
filled: true,
extruded: true,
elevationScale: this.state.elevationScale,
coverage: 50,
getHexagon: d => d.h3Location,
getFillColor: [223, 25, 149], // fluorescent pink
getElevation: d => {
console.log("d.price", d.price * 0.5);
return Number(d.price / 10);
}
})
];
};
// newData = testData;
// newData = eval(testData);
// _layer = new HexagonLayer({
// id: "hexagon-layer",
// data: testData,
// pickable: true,
// extruded: true,
// radius: 200,
// elevationScale: 100,
// // upperPercentile: 100,
// // getElevationValue: d =>
// getPosition: d => d.COORDINATES
// });
dataStateChange = () => {
window.setTimeout(
this.setState({
year: jan00
}),
2000
);
window.setTimeout(
this.setState({
year: jan05
}),
8000
);
};
// yearOnChange = e => {
// const data = {
// 0: jan95,
// 1: jan00,
// 2: jan05
// };
// window.setTimeout(() => {
// this.setState({
// location: data[e.currentTarget.value],
// year: e.currentTarget.value
// });
// }, 10000);
// };
yearOnChange = e => {
e.stopPropagation();
e.nativeEvent.stopImmediatePropagation();
const data = {
0: jan95,
1: jan00,
2: jan05
};
window.setTimeout(
() =>
this.setState({
location: data[e.currentTarget.value],
year: e.currentTarget.value
}),
10000
);
};
render() {
return (
<div
style={{
display: "flex",
justifyContent: "center",
alignItems: "center"
}}
>
<YearSelector
yearOnChange={this.yearOnChange}
year={this.state.year}
dataStateChange={this.dataStateChange}
/>
<DeckGL
// layers={this._layer}
layers={this._layerRendering()}
initialViewState={INITIAL_VIEW_STATE}
controller={true}
>
<StaticMap
reuseMaps
mapStyle={mapStyle}
MapController
preventStyleDiffing={true}
mapboxApiAccessToken={MAPBOX_TOKEN}
/>
</DeckGL>
</div>
);
}
}
export default FirstMap;
最佳答案
问题是您调用setState而不是给setTimeout
一个函数。这是您要寻找的:
yearOnChange = e => {
const data = {
0: jan95,
1: jan00,
2: jan05
};
window.setTimeout(
() => this.setState({
location: data[e.currentTarget.value],
year: e.currentTarget.value
}),
10000
);
};
关于javascript - setInterval/setTimeout可能导致页面重新加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57893297/