Closed slead closed 4 years ago
Never mind, I figured it out - passing the watchUtils response/promise to a separate function worked:
afterMapMove(evt) {
if (!this.state.checkboxStatus) {
this.setState({ buttonText: "Search this area" });
} else {
this.setState({ buttonText: "Search as I move the map" });
}
}
handleMapLoad(map, view) {
this.setState({ map: map, view: view });
loadModules(["esri/core/watchUtils"]).then(([watchUtils]) => {
watchUtils.whenTrue(view, "stationary", this.afterMapMove);
});
}
I'm asking the question here as my specific issue relates to React and ArcGIS Server JS API, but the issue may be more generally with esri-loader, or potentially the JS API itself(?)
My goal is to emulate the behaviour on the AirBnb map whereby the "search as I move the map" option changes its label if the option is not selected and the map extent changes. In that case it becomes a button with a manual "search this area" function.
In order to achieve this I'm using
watchUtils
and wish to change the option's label via the state. However, I can't access the state from within thewatchUtils
callback:Expected behavior How can I access the app's state from within the
watchUtils
callback?Actual behavior The
this
object is undefined inside the callback so the state cannot be accessed or changed.To Reproduce
Here is a simple example: https://codesandbox.io/s/watchutils-to-state-forked-ipn4o?file=/src/index.js
Versions (please complete the following information):