I'm facing a weird issue when trying to change a value of the state object after performing a GET request with either fetch or axios (not working with both). My code is pretty basic and I tried a lot of solutions that I found online but none of them is working.
This is the code I have so far ...
class FilterScreen extends React.Component {
state = {
selectedCity: null,
cities: [],
selectedSpecialty: null,
specialties: [],
};
componentWillMount() {
this.fetchCities();
}
fetchCities = () => {
fetch(`http://127.0.0.1:8000/app/cities`)
.then(res => res.json())
.then(e => {
this.setState({
results: e.data.map(item => ({
label: item.name,
value: item.id
}))
});
})
.catch(
() => (
alert('There was an issue while fetching the doctors.')
)
);
};
render() {
return (
... the body of the component...
)}
}
export default FilterScreen;
I really can't understand what I'm doing wrong here. I'm pretty new to React Native and maybe this has something to do with the lifecycles but I'm sure that componentWillMount is the right way to do it. Maybe I'm wrong ... Any kinda help is appreciated :D