I have a little problem with my app (made with react-native), when a user open the app i have the method componentDidMount which reload the state of latitutde and longitude by using this.setState.
After that i have a function that send the lat and long to my action. But when a user is connecting the time between the setState and the function call is pretty short, so my first API call render undefined because the state is not up to date before the API call. I don't know where to put my function to be called after the update of the state.
componentWillMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
lat: position.coords.latitude,
long: position.coords.longitude,
error: null,
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
);
setTimeout(() => {
const {lat, long} = this.state
console.log(lat)
this.props.getConditions(lat, long)
}, 1000)
}
even with the setTimeout the console.log return null (corresponding to my initial state)
Thanks for your help