Reactjs 反应缓存google放置api搜索结果
因此,我有以下功能组件,工作良好。我想做的是缓存结果,这样我就不会为了同一个搜索词而一遍又一遍地点击apiReactjs 反应缓存google放置api搜索结果,reactjs,caching,google-places-api,Reactjs,Caching,Google Places Api,因此,我有以下功能组件,工作良好。我想做的是缓存结果,这样我就不会为了同一个搜索词而一遍又一遍地点击api import React, { useState, useEffect, useRef } from "react"; import currentSession from "../currentSession"; let autoComplete; const loadScript = (url, callback) => { let
import React, { useState, useEffect, useRef } from "react";
import currentSession from "../currentSession";
let autoComplete;
const loadScript = (url, callback) => {
let script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) {
script.onreadystatechange = function () {
if (script.readyState === "loaded" || script.readyState === "complete") {
script.onreadystatechange = null;
callback();
}
};
} else {
script.onload = () => callback();
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
};
function handleScriptLoad(updateQuery, autoCompleteRef) {
autoComplete = new window.google.maps.places.Autocomplete(
autoCompleteRef.current,
{ types: ["(cities)"] }
);
autoComplete.setFields(["address_components", "formatted_address"]);
autoComplete.addListener("place_changed", () =>
handlePlaceSelect(updateQuery)
);
}
async function handlePlaceSelect(updateQuery) {
const addressObject = autoComplete.getPlace();
const query = addressObject.formatted_address;
updateQuery(query);
}
function SearchLocationInput(props) {
const [query, setQuery] = useState("");
const autoCompleteRef = useRef(null);
useEffect(() => {
loadScript(
"https://maps.googleapis.com/maps/api/js?key=" +
currentSession.getGoogleApiKey() +
"&libraries=places",
() => handleScriptLoad(setQuery, autoCompleteRef)
);
}, []);
return (
<div className="search-location-input">
<input
ref={autoCompleteRef}
onChange={(event) => {
props.onCityChange(event.target.value);
setQuery(event.target.value);
}}
placeholder="Enter a City"
value={query}
/>
</div>
);
}
export default SearchLocationInput;
import React,{useState,useffect,useRef}来自“React”;
从“./currentSession”导入currentSession;
让自动完成;
const loadScript=(url,回调)=>{
让script=document.createElement(“脚本”);
script.type=“text/javascript”;
if(script.readyState){
script.onreadystatechange=函数(){
如果(script.readyState==“已加载”| | script.readyState===“完成”){
script.onreadystatechange=null;
回调();
}
};
}否则{
script.onload=()=>callback();
}
script.src=url;
document.getElementsByTagName(“head”)[0].appendChild(脚本);
};
函数handleScriptLoad(updateQuery、autoCompleteRef){
autoComplete=newwindow.google.maps.places.autoComplete(
自动完成当前,
{类型:[(城市)]}
);
自动完成。设置字段([“地址组件”,“格式化地址]);
autoComplete.addListener(“place\u changed”,()=>
handlePlaceSelect(更新库)
);
}
异步函数handlePlaceSelect(updateQuery){
const addressObject=autoComplete.getPlace();
const query=addressObject.formatted\u地址;
更新(查询);
}
功能SearchLocationInput(道具){
const[query,setQuery]=useState(“”);
const autocompletef=useRef(null);
useffect(()=>{
加载脚本(
"https://maps.googleapis.com/maps/api/js?key=" +
currentSession.getGoogleAppKey()+
“&libraries=places”,
()=>handleScriptLoad(setQuery、autoCompleteRef)
);
}, []);
返回(
{
props.onCityChange(event.target.value);
setQuery(event.target.value);
}}
占位符=“输入城市”
值={query}
/>
);
}
导出默认SearchLocationInput;
我假设请求是在加载的脚本中发生的,因此我不确定如何才能完成此操作?您最终做了什么?因为这不可能,最终使用我自己的自动完成并使用本地存储缓存结果。例如,如果用户类型的“ab”api只有在缓存结果不在本地存储中时才会被调用。编写我自己的autocomplete组件非常简单,让我完全控制它的行为。你最终做了什么?因为这是不可能的,所以我最终使用我自己的autocomplete并使用本地存储缓存结果。例如,如果用户类型的“ab”api只有在缓存结果不在本地存储中时才会被调用。编写我自己的自动完成组件非常简单,让我完全控制它的行为。