1 回答

TA貢獻1830條經驗 獲得超9個贊
最后我usePosition在另一個組件中使用鉤子解決了這個問題,如下所示:
import React from "react";
import {usePosition} from "use-position";
const Geolocation = () => {
let currentPosition = usePosition();
return (
<div>
<p>{currentPosition.latitude}</p>
</div>
)
};
export default Geolocation;
在我的主要組件中,我使用了一個useState鉤子來控制渲染,如下所示:
const IndexPage = () => {
const [geolocation, setGeolocation] = useState('');
function handleGeolocation() {
setGeolocation(<Geolocation/>);
}
return (
<Layout>
<Card>
<Image/>
<div onClick={() => handleGeolocation()}>Click me to share my location</div>
{geolocation}
...
添加回答
舉報