mal*_*ers 84 javascript state reactjs
这是我的情况:
一个低于标准的解决方案:
请参阅下面的代码段:
handleFormSubmit: function(input){
// Form Input
this.setState({
originId: input.originId,
destinationId: input.destinationId,
radius: input.radius,
search: input.search
})
this.findRoutes();
},
handleMapRender: function(map){
// Intialized Google Map
directionsDisplay = new google.maps.DirectionsRenderer();
directionsService = new google.maps.DirectionsService();
this.setState({map: map});
placesService = new google.maps.places.PlacesService(map);
directionsDisplay.setMap(map);
},
findRoutes: function(){
var me = this;
if (!this.state.originId || !this.state.destinationId) {
alert("findRoutes!");
return;
}
var p1 = new Promise(function(resolve, reject) {
directionsService.route({
origin: {'placeId': me.state.originId},
destination: {'placeId': me.state.destinationId},
travelMode: me.state.travelMode
}, function(response, status){
if (status === google.maps.DirectionsStatus.OK) {
// me.response = response;
directionsDisplay.setDirections(response);
resolve(response);
} else {
window.alert('Directions config failed due to ' + status);
}
});
});
return p1
},
render: function() {
return (
<div className="MapControl">
<h1>Search</h1>
<MapForm
onFormSubmit={this.handleFormSubmit}
map={this.state.map}/>
<GMap
setMapState={this.handleMapRender}
originId= {this.state.originId}
destinationId= {this.state.destinationId}
radius= {this.state.radius}
search= {this.state.search}/>
</div>
);
}
});
Run Code Online (Sandbox Code Playgroud)
win*_*elt 208
setState()
有一个可选的回调参数,您可以使用它.您只需稍微更改一下代码即可:
// Form Input
this.setState(
{
originId: input.originId,
destinationId: input.destinationId,
radius: input.radius,
search: input.search
},
this.findRoutes // here is where you put the callback
);
Run Code Online (Sandbox Code Playgroud)
请注意,调用findRoutes
现在位于setState()
调用内部,作为第二个参数.
没有()
因为你传递的功能.
Fur*_*uan 58
如果这里有人登陆并使用钩子遇到相同的情况,则可以通过以下过程实现相同的行为
const [data, setData] = useState(false);
useEffect(() => {
doSomething(); // This will be executed when the state changes
}, [data]);
setData(true);
Run Code Online (Sandbox Code Playgroud)
这里useEffect
将在数据发生任何更改后运行,我们可以执行任何相关任务。
小智 12
this.setState(
{
originId: input.originId,
destinationId: input.destinationId,
radius: input.radius,
search: input.search
},
function() { console.log("setState completed", this.state) }
)
Run Code Online (Sandbox Code Playgroud)
这可能会有所帮助
根据setState()
新状态的文档可能不会反映在回调函数中findRoutes()
.以下是React docs的摘录:
setState()不会立即改变this.state,但会创建挂起状态转换.调用此方法后访问this.state可能会返回现有值.
无法保证对setState的调用进行同步操作,并且可以对调用进行批处理以获得性能提升.
所以这就是我建议你应该做的.您应该input
在回调函数中传递新状态findRoutes()
.
handleFormSubmit: function(input){
// Form Input
this.setState({
originId: input.originId,
destinationId: input.destinationId,
radius: input.radius,
search: input.search
});
this.findRoutes(input); // Pass the input here
}
Run Code Online (Sandbox Code Playgroud)
该findRoutes()
函数应该像这样定义:
findRoutes: function(me = this.state) { // This will accept the input if passed otherwise use this.state
if (!me.originId || !me.destinationId) {
alert("findRoutes!");
return;
}
var p1 = new Promise(function(resolve, reject) {
directionsService.route({
origin: {'placeId': me.originId},
destination: {'placeId': me.destinationId},
travelMode: me.travelMode
}, function(response, status){
if (status === google.maps.DirectionsStatus.OK) {
// me.response = response;
directionsDisplay.setDirections(response);
resolve(response);
} else {
window.alert('Directions config failed due to ' + status);
}
});
});
return p1
}
Run Code Online (Sandbox Code Playgroud)
小智 6
setState
采用新状态和可选的回调函数,在状态更新后调用。
this.setState(
{newState: 'whatever'},
() => {/*do something after the state has been updated*/}
)
Run Code Online (Sandbox Code Playgroud)