英文:
Component won't render on submit
问题
以下是您要翻译的内容:
"Hope you can help me with this. So I am trying to use IP address to get latitude and longitude and therefore get a location on a map when a new IP is submitted using axios
and react-query
but can't find out what am I doing wrong.
I tried with only useState and then setting setIp(data);
to handleSubmit
but it didn't work as well.
I always get undefined
data when refreshing the page so I handled that like this { geoData && <CreateMap ...
Is it even the right approach with react-query? Is this how I fetch the data?
Thank you for your help!"
"I have 2 components called Input and CreateMap.
Input.jsx:
import React, { useState } from "react";
import axios from "axios";
import CreateMap from "./CreateMap";
import { useQuery } from "@tanstack/react-query";
const InputComponent = () => {
const [ip, setIp] = useState("8.8.8.8");
const [geoData, setGeoData] = useState();
const { isLoading, isError, data, error } = useQuery({
queryKey: ["posts"],
queryFn: async () => {
try {
const response = await axios.get(
https://api.ipgeolocation.io/ipgeo?apiKey=e1ba80b5c7a64f0fb61db1f286d78562&ip=${ip}
);
return response.data;
} catch (error) {
throw new Error(error);
}
},
});
if (isLoading) {
return
Loading...
;
}
if (isError) {
return
Error: {error.message}
;
}
const handleIpChange = (e) => {
setIp(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
setGeoData(data)
};
return (
);
};
export default InputComponent;
CreateMap.jsx
import React from "react";
import { MapContainer, Marker, Popup, TileLayer } from "react-leaflet";
import { Icon } from "leaflet";
import "./Map.css";
const CreateMap = ({ lat, lng }) => {
return (
style={{ height: "480px", width: "100%" }}
center={[lat, lng]}
zoom={12}
>
);
};
export default CreateMap;
and App.js
import React from 'react'
import Input from './components/Input'
const App = () => {
return (
App
)
}
export default App"
英文:
Hope you can help me with this. So I am trying to use IP address to get latitude and longitude and therefore get a location on a map when an new IP is submitted using axios
and react-query
but can't find out what am I doing wrong.
I tried with only useState and then setting setIp(data);
to handleSubmit
but it didn't work as well.
I always get undefined
data when refresh page so I handled that like this { geoData && <CreateMap ...
Is it even right approach with react-query? Is this how I fetch the data?
Thank you for your help!
I have 2 components called Input and CreateMap.
Input.jsx:
import React, { useState } from "react";
import axios from "axios";
import CreateMap from "./CreateMap";
import { useQuery } from "@tanstack/react-query";
const InputComponent = () => {
const [ip, setIp] = useState("8.8.8.8");
const [geoData, setGeoData] = useState();
const { isLoading, isError, data, error } = useQuery({
queryKey: ["posts"],
queryFn: async () => {
try {
const response = await axios.get(
`https://api.ipgeolocation.io/ipgeo?apiKey=e1ba80b5c7a64f0fb61db1f286d78562&ip=${ip}`
);
return response.data;
} catch (error) {
throw new Error(error);
}
},
});
if (isLoading) {
return <h1>Loading...</h1>;
}
if (isError) {
return <h1>Error: {error.message}</h1>;
}
const handleIpChange = (e) => {
setIp(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
setGeoData(data)
};
return (
<form onSubmit={handleSubmit}>
<label>Write your IP address: </label>
<input type="text" defaultValue={ip} onChange={handleIpChange} />
<button type="submit">Submit</button>
{ geoData && <CreateMap lat={geoData.latitude} lng={geoData.longitude} /> }
</form>
);
};
export default InputComponent;
CreateMap.jsx
import React from "react";
import { MapContainer, Marker, Popup, TileLayer } from "react-leaflet";
import { Icon } from "leaflet";
import "./Map.css";
const CreateMap = ({ lat, lng }) => {
return (
<div>
<MapContainer
style={{ height: "480px", width: "100%" }}
center={[lat, lng]}
zoom={12}
>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
</MapContainer>
</div>
);
};
export default CreateMap;
and App.js
import React from 'react'
import Input from './components/Input'
const App = () => {
return (
<div>
<h1>App</h1>
<Input />
</div>
)
}
export default App
答案1
得分: 2
以下是您提供的代码的翻译部分:
这里应该使用 `react-query` 中的 `refetch`,类似这样就可以了
const InputComponent = () => {
const [ip, setIp] = useState("8.8.8.8");
const { isLoading, isError, data, error, refetch } = useQuery({
queryKey: ["posts"],
queryFn: async () => {
try {
const response = await axios.get(
`https://api.ipgeolocation.io/ipgeo?apiKey=e1ba80b5c7a64f0fb61db1f286d78562&ip=${ip}`
);
return response.data;
} catch (error) {
throw new Error(error);
}
},
enabled: false
});
if (isLoading) {
return <h1>Loading...</h1>;
}
if (isError) {
return <h1>Error: {error.message}</h1>;
}
const handleIpChange = (e) => {
setIp(e.target.value);
};
const handleSubmit = async (e) => {
e.preventDefault();
refetch();
};
return (
<form onSubmit={handleSubmit}>
<label>输入您的IP地址: </label>
<input type="text" defaultValue={ip} onChange={handleIpChange} />
<button type="submit">提交</button>
{ data && <CreateMap lat={data.latitude} lng={data.longitude} /> }
</form>
);
};
对于重新渲染,创建一个控制组件:
function UpdateCenter({ center }) {
const map = useMap();
map.setView(center);
return null;
}
并将其添加到您的 CreateMap 组件中:
const CreateMap = ({ lat, lng }) => {
return (
<div>
<MapContainer
style={{ height: "480px", width: "100%" }}
center={[lat, lng]}
zoom={12}
>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
<UpdateCenter center={[lat, lng]} />
</MapContainer>
</div>
);
};
请注意,我已经将HTML标签和JavaScript代码进行了翻译。如果您需要更多帮助或有其他问题,请告诉我。
英文:
Something like this should be good with the refetch
from react-query
const InputComponent = () => {
const [ip, setIp] = useState("8.8.8.8");
const { isLoading, isError, data, error, refetch } = useQuery({
queryKey: ["posts"],
queryFn: async () => {
try {
const response = await axios.get(
`https://api.ipgeolocation.io/ipgeo?apiKey=e1ba80b5c7a64f0fb61db1f286d78562&ip=${ip}`
);
return response.data;
} catch (error) {
throw new Error(error);
}
},
enabled: false
});
if (isLoading) {
return <h1>Loading...</h1>;
}
if (isError) {
return <h1>Error: {error.message}</h1>;
}
const handleIpChange = (e) => {
setIp(e.target.value);
};
const handleSubmit = async (e) => {
e.preventDefault();
refetch();
};
return (
<form onSubmit={handleSubmit}>
<label>Write your IP address: </label>
<input type="text" defaultValue={ip} onChange={handleIpChange} />
<button type="submit">Submit</button>
{ data && <CreateMap lat={data.latitude} lng={data.longitude} /> }
</form>
);
};
For the rerender, create a control component :
function UpdateCenter({ center }) {
const map = useMap();
map.setView(center);
return null;
}
And add it in your CreateMap :
const CreateMap = ({ lat, lng }) => {
return (
<div>
<MapContainer
style={{ height: "480px", width: "100%" }}
center={[lat, lng]}
zoom={12}
>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
<UpdateCenter center={[lat, lng]} />
</MapContainer>
</div>
);
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论