Having trouble handling the response from an HTTP request using Axios with TypeScript. I don't understand the behavior I'm seeing. This returns the location once, and then when I refresh the page it doesn't work again, it says TypeError: Cannot read property 'location' of undefined
I have a feeling I'm not understanding how this all works.
All I'm trying to do is get access all the data returned from the API. If anyone has simple examples of doing it with TypeScript I'd really appreciate it. I can do this in normal JavaScript, but having trouble translating it over to TypeScript. My problem is similar to this post
const Webmap: FC = () => {
const url = 'https://data.police.uk/api/crimes-street/all-crime?lat=52.629729&lng=-1.131592&date=2019-10';
interface User {
id?: any;
location?: any;
}
const [users, setUserList] = useState<User[]>([]);
useEffect(() => {
axios.get<User[]>(url)
.then(response => {
// console.log(response.data);
setUserList(response.data);
});
}, [])
console.log(users[0].location)