So I was trying to fetch some anime data based on the options I have in select (genre),
the problem is that the fetch doesn't happen onChange in my select only after changing option twice, I have tried to use useEffect
but the problem with that it's fetching immediately without changing the select value.
can someone help and explain what I'm doing wrong?
here is my code on codesandbox
index.js
function App() {
const [list,setList]=useState([])
const [genre,setGenre]=useState(0)
const fetchData=()=>{
const options = {
method: 'GET',
url: `https://jikan1.p.rapidapi.com/genre/anime/${genre}/1`,
headers: {
'x-rapidapi-host': 'jikan1.p.rapidapi.com',
'x-rapidapi-key': '*******************************'
}
};
axios.request(options).then(function (response) {
console.log(response.data.anime);
setList(response.data.anime)
}).catch(function (error) {
console.error(error);
});
}
const handleChange=(e)=>{
setGenre(e.target.value)
fetchData()
}
return (
<div className="mx-auto px-8 py-4 flex flex-col justify-center">
<div>
<h1>Design with Tailwind</h1>
<select name="genre" className='p-2 bg-bg-prime' id="genre" onChange={handleChange}>
<option value="1" >Action</option>
<option value="2" >Adventure</option>
<option value="4" >Comedy</option>
</select>
</div>
<div>
{list && (
list.map((an)=>(
<p key={an.title}>{an.title}</p>
))
)}
</div>
</div>
)
}
const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)