This tutorial shows how to load function using the React useEffect only once.

 Example 1: Loading Function Using React Only Once

You can give an empty array to the useEffect as a second argument to load the function only once. Below is the code:

function MyComponent() {
  useEffect(() => {
  }, []);

   <div>{/* ... */}</div>;

Example 2:

function App() {
const [user, setUser] = React.useState(null);

React.useEffect(() => {
.then(results => results.json())
.then(data => {
}, []); 

return <div>
{user ? : 'Loading...'}

ReactDOM.render(<App/>, document.getElementById('app'));
<script src=""></script>
<script src=""></script>

<div id="app"></div>

Vinish Kapoor

An Oracle Apex Consultant, Oracle ACE, and founder of and a question and answer forum for developers.

Leave a Reply