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>
);
}
Hooks can only be called inside the body of a function component. There are three common reasons you might be seeing it: You might have mismatching versions of React and React DOM. You might be breaking the Rules of Hooks.
Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any early returns. By following this rule, you ensure that Hooks are called in the same order each time a component renders.
You can not use hooks outside a component function, it is simply how they work. But, you can make a composition of hooks. React relies on an amount and order of how hooks appear in the component function.
You can't use Hooks inside a class component, but you can definitely mix classes and function components with Hooks in a single tree. Whether a component is a class or a function that uses Hooks is an implementation detail of that component.
If your goal is to trigger data fetching upon clicking on a button, there's no need for useEffect
. useEffect
is used to replace the lifecycle hooks - componentDidMount
, componentDidUpdate
, and componentWillUnmount
.
Think about this - without hooks, will you need these lifecycle hooks at all to achieve what you want? With classes, you simply have to use this.state
and a callback that triggers upon clicking of the button; you wouldn't use any of these lifecycle hooks.
The side effect you want to achieve is data fetching, but that's a user-triggered effect and useEffect
is not helpful here. useEffect
is only helpful when you want to have side effects during the lifecycle of the component.
The code below should do what you want to achieve with hooks, and you don't need useEffect
, you only need useState
.
function App() {
const [user, setUser] = React.useState(null);
function fetchData() {
fetch('https://randomuser.me/api/')
.then(results => results.json())
.then(data => {
setUser(data.results[0]);
});
};
return <div>
<p>{user ? user.name.first : 'No data'}</p>
<button onClick={fetchData}>Fetch Data</button>
</div>;
}
ReactDOM.render(<App/>, document.getElementById('app'));
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<div id="app"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With