function Search() {
let [state, setState] = useState({
query: "",
results: []
})
let handleSubmit = e => {
e.preventDefault();
if(state.query.length>0) {
fetch(searchURL)
.then(response => response.json())
.then(data => setState(prevValue => {
return {
...prevValue,
results: data.Search
}
}))
setState({query:""})
}
}
return (
<div>
<h1>Search</h1>
<form onSubmit={handleSubmit}>
<input type="text" placeholder="Search..." onChange={handleChange} value={state.query}/>
<button type="submit">Search</button>
</form>
</div>
)
}
After submitting the form, if I am setting the state, as in setState{query:""} , the API call is failing. How to acheive this. Thanks