I am trying to get api call on click, but I’m stuck with my console just showing null. I want on different items clicks, different api categories would be called.
const [category, setCategory] = useState("");
useEffect(() => {
axios
.get(`${url}${category}`)
.then(function (response: any) {
console.log(response.data.meals);
})
.catch(function (error: any) {
console.log(error);
});
}, [category]);
const onClickHandler = (e: any) => {
setCategory(e.target.value);
};
<li value="Seafood" onClick={onClickHandler}>
Seafood
</li>
I was expecting like filter buttons to click, and then items would show up based on the filter you click.
2
Answers
I don’t know much but maybe it can work like this, I apologize if it is wrong.
The
value
property of ali
element is reserved, and it tells the order of theli
in its parentol
, as you can read on mdn:You would wanna use a
button
, which will work with your attempt and is made for handling clicks. You could nest it in yourli
. But if, for some reason, you want only theli
, I would suggest a data attribute to avoid confusion: