I am new to using APIs with React Native, I have a few things figured out but what I'm trying to do now is create a Search function. I've tried a few things so far, but either the function isn't being called, or I get an error about the function being called as an object. Also, I am using axios to make this API call
Here's what I've got so far.
export default class CategoryScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [],
isVisible: true,
city: '280',
isLoading: true,
search: ''
}
}
async componentDidMount() {
try {
const id = this.props.navigation.state.params.category;
const city = this.state.city;
const result = await axios.request({
method: 'get',
url: `https://developers.zomato.com/api/v2.1/search?city_id=${city}&category=${id}`,
headers: {
'Content-Type': 'application/json',
'user-key': 'a31bd76da32396a27b6906bf0ca707a2'
}
});
this.setState({ data: result.data.restaurants });
} catch (err) {
console.log(err);
} finally {
this.setState({ isLoading: false });
}
}
updateSearch = search => {
this.setState({ search });
};
searchReq = () => {
axios.request({
method: 'get',
url: `https://developers.zomato.com/api/v2.1/search?city_id=${city}&q${search}&category=${id}`,
header: {
'Content-Type': 'application/json',
'user-key': 'a31bd76da32396a27b6906bf0ca707a2'
}
});
this.setState({ data: result.data.restaurants });
}
render() {
const { search } = this.state;
return (
<SearchBar
placeholder="Type Here..."
onChangeText={this.updateSearch}
onSubmitEditing={this.searchReq}
value={search}
/>
)
}
}
If there is a more advanced way of doing it I'm open to learning something new, I just want to accomplish what I want to do. Also, I know that if my current method worked it'd still cause problems if the user adds a space in between, so I'm very open to learning new and more advanced ways of doing this
Here is the expo snack https://snack.expo.io/SJoIr8u1I