Intro to React: Core Hooks and Basics

A calm walk through React’s mental model and the core hooks you will actually use day to day.

I still remember the first time React clicked for me.

It was not a big “aha” with fireworks. It was quieter than that. I had a button. I clicked it. A number on the screen went up. And for a second I thought: oh. The UI is just a function of state.

That sentence sounds fancy, but the idea is simple. You describe what the screen should look like for a given piece of data. When the data changes, React redraws the parts that need to change. You stop chasing the DOM by hand. You stop writing “find this node, then change that text.” You write the picture you want, and React keeps the picture honest.

This post is a walk through that idea, and through the core hooks that make it useful. Not every hook. Just the ones that show up in real work.

First, the basic mental model

A React app is made of components. A component is usually a function that returns UI.

function Greeting({ name }) {
  return <h1>Hello, {name}</h1>;
}

name here is a prop. Props are inputs from the parent. They flow one way: down. If a child needs to tell the parent something happened, the parent usually passes a function as a prop, and the child calls it.

That one-way flow is not a rule for the sake of rules. It makes the story of your UI easier to follow. Data comes in. Events go out. You always know where to look.

Components can nest. A page can be a tree of small pieces. Each piece owns a job. When something changes, React figures out what to update. You do not have to.

But components alone are not enough. UI needs memory. Buttons get clicked. Forms get typed into. Data arrives from a server. That is where hooks come in.

useState: give the UI a short-term memory

useState is the hook you meet first, and for good reason. It holds a value that can change over time.

import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <button onClick={() => setCount(count + 1)}>
      Clicked {count} times
    </button>
  );
}

count is the current value. setCount is how you ask for a new value. When you call setCount, React schedules a re-render with the new state.

A few habits that save pain later:

  • Treat state as a snapshot for this render. Do not mutate it in place.
  • If the next value depends on the previous one, prefer the updater form: setCount((c) => c + 1).
  • Keep state as small as you can. Derive what you can during render instead of storing copies.

Think of useState as a sticky note on the component. React keeps the note between renders. You rewrite the note when something happens.

useEffect: talk to the world outside React

Some work does not belong in the render itself. Fetching data. Setting up a subscription. Syncing with localStorage. Talking to a browser API.

That is useEffect.

import { useEffect, useState } from 'react';

function UserProfile({ userId }) {
  const [user, setUser] = useState(null);

  useEffect(() => {
    let cancelled = false;

    async function load() {
      const res = await fetch(`/api/users/${userId}`);
      const data = await res.json();
      if (!cancelled) setUser(data);
    }

    load();

    return () => {
      cancelled = true;
    };
  }, [userId]);

  if (!user) return <p>Loading…</p>;
  return <h2>{user.name}</h2>;
}

The effect runs after paint. The dependency array tells React when to run it again. The cleanup function runs before the next effect, or when the component leaves the screen.

A useful way to think about it: render is for calculating UI. Effects are for synchronizing with something outside that calculation.

A common trap is putting too much in effects. If you can compute a value from props and state during render, do that. Effects are for side effects, not for reshaping data you already have.

useRef: a box that does not cause re-renders

Sometimes you need a value that survives renders, but changing it should not redraw the UI. A timer id. A DOM node. A flag that says “this request is old now.”

That is useRef.

import { useEffect, useRef } from 'react';

function TextField() {
  const inputRef = useRef(null);

  useEffect(() => {
    inputRef.current?.focus();
  }, []);

  return <input ref={inputRef} placeholder="Type here" />;
}

ref.current is mutable. You can write to it any time. React will not re-render just because you changed it. That is the point.

Use refs when you need an escape hatch to the DOM, or when you need to remember something without turning it into state.

useMemo and useCallback: only when the cost is real

These two hooks get overused. A lot.

useMemo caches a computed value. useCallback caches a function identity.

import { useMemo, useCallback } from 'react';

function ProductList({ products, query, onSelect }) {
  const filtered = useMemo(() => {
    return products.filter((p) =>
      p.name.toLowerCase().includes(query.toLowerCase())
    );
  }, [products, query]);

  const handleSelect = useCallback(
    (id) => {
      onSelect(id);
    },
    [onSelect]
  );

  return (
    <ul>
      {filtered.map((product) => (
        <li key={product.id}>
          <button onClick={() => handleSelect(product.id)}>
            {product.name}
          </button>
        </li>
      ))}
    </ul>
  );
}

When do they help?

  • The calculation is expensive, and you can prove it.
  • A child is memoized and re-renders too often because a new function or object is created every render.

When do they not help?

  • Almost every normal calculation.
  • Almost every event handler in a small component.

React is already fast for most UI. Memoization adds mental weight. Start without it. Add it when you have a real reason, not because a checklist said so.

useContext: share values without prop drilling

Sometimes many components need the same thing: theme, current user, locale. Passing that value through five layers of props gets noisy.

Context lets a parent provide a value that deep children can read.

import { createContext, useContext } from 'react';

const ThemeContext = createContext('light');

function App() {
  return (
    <ThemeContext.Provider value="dark">
      <Page />
    </ThemeContext.Provider>
  );
}

function ThemedButton() {
  const theme = useContext(ThemeContext);
  return <button data-theme={theme}>Save</button>;
}

Context is great for values that feel “global” for a subtree. It is less great as a dumping ground for every piece of state in the app. When everything is context, updates get harder to reason about, and re-renders can spread wider than you expect.

A simple rule I like: props for local relationships. Context for ambient values. A store or server state library when the data story gets bigger.

Putting it together

Here is the story again, in short form.

  • Components describe UI.
  • Props pass data down.
  • useState holds local memory.
  • useEffect syncs with the outside world.
  • useRef keeps a mutable box without re-rendering.
  • useMemo / useCallback cache work or identities when the cost is real.
  • useContext shares ambient values across a tree.

You do not need all of these on day one. Most screens start with props, a little state, and maybe one effect. The rest show up when the problem asks for them.

A closing thought

React feels hard when you try to learn every API at once. It feels calmer when you treat it like a story:

What is on the screen?
What data does that screen need?
What events change that data?
What outside systems must stay in sync?

Answer those, and the hooks start to feel less like magic and more like tools with clear jobs.

If you are learning React right now, build something small. A counter. A todo list. A tiny dashboard that fetches one API. Click around. Break it. Fix it. That loop teaches faster than any list of hooks ever will.

And when something finally clicks, it might not feel dramatic. It might just feel quiet and clear. That is usually the good kind of progress.