How do I trigger useEffect() on a button click, rather than on rendering the component?
When I use it inside a function like this, I get an error stating:
Hooks can only be called inside the body of a function component
function App() {
function buttonClicked() {
useEffect(() => {
// Fetch from API
});
}
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">App Title</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
<button onClick={buttonClicked}>Make API Call</button>
</div>
);
}