liveBook · Manning

Side effect

As explained in React Quickly, Second Edition

A side effect is an observable change or interaction caused by evaluating code beyond returning a value.

A side effect is an observable interaction caused by evaluating a component beyond returning its rendered result. In React, useEffect places this work alongside the component lifecycle instead of performing it directly during rendering. section 6.1.4 section 4.2.1

Use an effect when component work must synchronize with something outside its immediate render:

  • Run once on mount by passing an empty dependency array, such as loading remote options for a drop-down. section 6.1.1
  • Run when selected values change by listing them in the dependency array, such as updating document.title when a blog title changes. section 6.1.4
  • Return a cleanup function when external work may outlive the component, such as a request continuing after unmount. section 6.1.1

Synchronizing the document title

function BlogPost({ title, body }) { useEffect(() => { document.title = title; }, [title]); return <article><h1>{title}</h1>{body}</article>; } The effect runs on mount and whenever title changes, but not merely because another property changes. section 6.1.4

Listing 6.3 Side effect executed in hook
import { useEffect } from "react";
function BlogPost({ title, body }) {
  useEffect(() => {
    document.title = title;    #1
  }, [title]);                 #2
  return (
    <article>
      <h1>{title}</h1>
      {body}
    </article>
  );
}
function App() {
  return (
    <main>
      <BlogPost title="First post" body={
        <p>Welcome to my cool website.</p>
      } />
    </main>
  );
}
export default App;

Inspect the complete BlogPost component and its dependency array. section 6.1.4

How React Quickly, Second Edition covers it

How deep it goes
Explains mount, update, dependency, and cleanup timing through runnable examples, but does not develop effect internals.
Examples
Demonstrates remote drop-down loading, document-title updates, and an email input synchronized from a property.
What it assumes
Assumes functional components, properties, and state while using hooks to make effects easier to isolate and test.
What you can do after
Apply it via React's useEffect to schedule synchronization on mount or updates and recognize when cleanup is needed.
How it connects
Connects side effects with React, useEffect, dependency arrays, component lifecycle, props, state, and controlled inputs.

Also explained in

Compare how 24 books explain side effect →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.