我用这段代码得到了一个无限循环。
我一直在尝试其他帖子中的一些解决方案,但它们不起作用。
locationAddress 是一个地址数组,我正在尝试使用 Google Maps Geocode API 获取坐标。
const reducer = (state, action) => {
switch (action.type) {
case 'add':
return [
...state,
{
address: action.address,
name: action.name,
id: action.id
}
];
case 'remove':
return state.filter((_, index) => index !== action.index)
default:
return state;
}
}
const [locationAddress, setLocationAddress] = useReducer(reducer, []);
const [coordinates, setCoordinates] = useState([]);
useEffect(() => {
const fetchLocation = async () => {
for(let i = 0; i < locationAddress.length; i++) {
const response = …Run Code Online (Sandbox Code Playgroud)