.Net application development specialists
asp.net, c#, vb.net, html, javascript, jquery, html, xhtml, css, oop, design patterns, sql server, mvc and much more
contact: admin@paxium.co.uk

Paxium is the company owned by myself, Dave Amour and used for providing IT contract development services including


  • Application development - Desktop, Web, Services - with Classic ASP, Asp.net WebForms, Asp.net MVC, Asp.net Core
  • Html, Css, JavaScript, jQuery, React, C#, SQL Server, Ado.net, Entity Framework, NHibernate, TDD, WebApi, GIT, IIS
  • Database schema design, implementation & ETL activities
  • Website design and hosting including email hosting
  • Training - typically one to one sessions
  • Reverse Engineering and documentation of undocumented systems
  • Code Reviews
  • Performance Tuning
  • Located in Cannock, Staffordshire
Rugeley Chess Club Buying Butler Cuckooland Katmaid Pet Sitting Services Roland Garros 60 60 Golf cement Technical Conformity Goofy MaggieBears Vacc Track Find Your Smart Phone eBate Taylors Poultry Services Lafarge Rebates System Codemasters Grid Game eBate DOFF

When to use useEffect vs a Click Handler

User Question: So given useEffect is for side effects, if I want to load some json from an endpoint when a button is clicked, can I do that in the click handler or should I use a useEffect?

Answer:

Good question πŸ‘

It depends on when you want the fetch to happen:

πŸ”Ή Using useEffect

- Use this when you want data to load automatically as a side-effect of rendering (e.g., when the component first mounts or when some dependency changes).
- Example: load movies from an API when the component is displayed.

import { useEffect, useState } from "react";

function Movies() {
  const [movies, setMovies] = useState([]);

  useEffect(() => {
    fetch("/api/movies")
      .then(res => res.json())
      .then(data => setMovies(data));
  }, []); // empty array β†’ run once on mount

  return <div>{movies.length} movies loaded</div>;
}

Here useEffect is right, because fetching is a side-effect of rendering.

πŸ”Ή Using a Click Handler

- Use this when you only want the fetch to happen in direct response to user action (like clicking a button).
- You don’t need useEffect at all β€” just call fetch in the handler.

import { useState } from "react";

function MovieLoader() {
  const [movies, setMovies] = useState([]);

  const handleClick = () => {
    fetch("/api/movies")
      .then(res => res.json())
      .then(data => setMovies(data));
  };

  return (
    <>
      <button onClick={handleClick}>Load Movies</button>
      <div>{movies.length} movies loaded</div>
    </>
  );
}

Here, useEffect would be wrong, because the fetch isn’t tied to rendering β€” it’s tied to the button click.

βœ… Rule of thumb:

  • If the fetch happens because the component exists or state/props changed β†’ useEffect.
  • If the fetch happens because the user explicitly did something β†’ event handler.

Explaining res and data

User Question: Explain res and data here

const handleClick = () => {
  fetch("/api/movies")
    .then(res => res.json())
    .then(data => setMovies(data));
};

Answer:

  • fetch("/api/movies") β†’ makes an HTTP request and returns a Promise that resolves to a Response object.
  • res β†’ the HTTP Response object. It has:
    • res.ok β†’ true if status 200–299
    • res.status β†’ numeric status code
    • res.headers β†’ response headers
    • methods like .json(), .text(), .blob()
  • res.json() β†’ reads the body and parses it as JSON. Returns a Promise resolving to JS data.
  • data β†’ the parsed JSON you actually use (e.g., an array of movies).

βœ… So in plain English:

  • res = the HTTP response wrapper (status, headers, body, etc.).
  • res.json() = extracts and parses the body as JSON.
  • data = the actual parsed JSON you care about.

Example Article Snippet Using then()

Click the button to fetch /api/movies. Here, res is the HTTP Response (status, headers, body reader), and data is the parsed JSON from res.json().

<button id="load-movies">Load Movies</button>
<div id="status"></div>
<ul id="movie-list"></ul>

<script>
  const btn = document.getElementById('load-movies');
  const statusEl = document.getElementById('status');
  const listEl = document.getElementById('movie-list');

  btn.addEventListener('click', () => {
    statusEl.textContent = 'Loading...';

    fetch('/api/movies', { headers: { 'Accept': 'application/json' } })
      .then(res => {
        if (!res.ok) {
          throw new Error(`HTTP ${res.status} ${res.statusText}`);
        }
        return res.json();
      })
      .then(data => {
        listEl.innerHTML = Array.isArray(data)
          ? data.map(m => `<li>${m.title ?? '(untitled)'}</li>`).join('')
          : `<li>Unexpected payload: ${JSON.stringify(data)}</li>`;

        statusEl.textContent = Array.isArray(data)
          ? `${data.length} movies loaded`
          : 'Loaded (non-array response)';
      })
      .catch(err => {
        statusEl.textContent = `Failed: ${err.message}`;
      });
  });
</script>