let lon, lat;
let weather;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
lat = position.coords.latitude;
lon = position.coords.longitude;
console.log(typeof lat)
});
}
class Button extends React.Component{
state = {latt: lat}
\t \t render(){
\t \t \t return(
\t \t <div>
{this.state.latt}
<h1>ss</h1>
</div>
\t \t \t
)}}
class Appp extends React.Component {
\t render(){
\t \t return(
\t \t \t <Button />
\t \t)}
\t }
ReactDOM.render(<Appp />, mountNode)
当我CONSOLE.LOG可变我得到一个号码,但为什么不能我用它作为状态的值。即使将它放入数组也没有区别。
请相关的代码添加到您的问题改变的状态。处理外部图像很困难。 –
你可以在这里发布你的代码来帮助调试吗? – Rowland
由于'getCurrentPosition'不同步,所以当lat被调用时,你的lat,lon不会被填充。相反,您应该在组件中移动此代码并使用'setState'来设置lat和long。 – Panther