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}
...
添加回答
举报