I am trying to make an API call inside a functional component, based on a form submission:
const SearchForm = () => {
const [keywords, setKeywords] = useState('')
const [fetchedData, setFetchedData] = useState('')
const handleSubmit = (e) => {
e.preventDefault();
useEffect(() => {
async function fetchData() {
const {data} = await axios.post('http://127.0.0.1:8000/api/posts/', keywords)
setFetchedData(data);
}
fetchData()
})
}
return (
<div>
<form onSubmit={ handleSubmit }>
<div className='input-field'>
<input placeholder="Search whatever you wish"
type="text"
value={keywords}
onChange={(e) => setKeywords(e.target.value)}
/>
</div>
</form>
</div>
)
}
However when I try this, the following error shows up:
React Hook "useEffect" is called in function "handleSubmit" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
How do I carry this out?