I have the code as below which changes the values of cords inside the state if the device location is available.
class App extends Component{
state={
cords:{
longitude:24.01,
latitude:38.22
},
data:{}
}
componentDidMount() {
console.log("Mounted")
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position=>{
let newCords={
latitude: position.coords.latitude,
longitude: position.coords.longitude
}
this.setState({cords:newCords});
console.log("Inside",this.state);//displays new values
});
};
console.log(this.state);// displays old values
The change in state() can be observed only inside the arrow function. How to get the changed values outside the whole if-block?