I am trying to make this class component into a functional one that just sends back the longitude and latitude. the class component does give me long/lat but I want to use it as a function. The function should just return the longitude and latitude.
I have tried to make it a function with those functions inside of it but when the call back function getCoords is called in the navigator.getcurrent.... it gives an error saying no assignment.
import React, { Component } from 'react';
class App extends Component {
constructor(props) {
super(props);
this.state = {
lat: '',
long: ''
};
}
getLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.getCoords)
} else {
alert('GeoLocation not enabled');
}
}
getCoords = pos => {
console.log(pos)
this.setState({
lat: pos.coords.latitude,
long: pos.coords.longitude
})
}
render() {
return (
<div>
<button onClick={this.getLocation}>Click me</button>
<p>lat: {this.state.lat}</p>
<p>long {this.state.long}</p>
</div>
);
}
}
export default App;
Do I have to learn react hooks in order to solve my functional component problem?