I am trying to make a request using useFetch from react-fetch-hook npm package when a button is pressed. Here is my code
import React, { useState } from 'react';
import useFetch from 'react-fetch-hook';
import { Button } from 'react-bootstrap';
const Component = () => {
const [message, setMessage] = useState(false);
const handleOnClick = () => {
const { isLoading, data } = useFetch(
'https://raw.githubusercontent.com/markmclaren2/sample_json/main/user.json'
);
if (isLoading) {
setMessage('Loading...');
} else {
setMessage(`Received id: ${data.id}`);
}
};
return (
<div>
<Button onClick={handleOnClick}>Load Data</Button>
<div>{message}</div>
</div>
);
};
export default Component;
I encountered this error
React Hook "useFetch" is called in function "handleOnClick" that is neither a React function component nor a custom React Hook function.
What is the correct way to call useFetch when the user presses the button and show the "Loading..." and then the result messages?