I am trying to implement a search bar. After filling in the input text field, an empty array is being returned when I am clicking on the search button for the first time. However on subsequent clicks, correct data is being returned. Why is this happening?
Sharing snippets of my code:
function Search(){
const [person, setPerson] = useState('');
const [movie_res, setMovie_res] = useState([])
const onClickHandler = async () =>{
axios.get('https://api.themoviedb.org/3/search/movie?api_key=***&language=en-US&query=' + person + '&page=1&include_adult=false').then(
res => {
setMovie_res(res.data.results)
console.log(movie_res);
}
).catch('SEARCH ERROR!!')
}
return(
...
<TextField id="filled-basic" label="Search" variant="filled" onChange = {event => setPerson(event.target.value)} />
<Button onClick = {onClickHandler}>In Movies </Button>
)
}