# Signal Computed & Effects

> Eliminate dependency arrays with automatic runtime tracking.

Subject: State Management | Lesson 22 of 22 | 6 min read
Interactive lesson: https://codewithanimation.in/state-management/signals-computed-and-effects
Previous: [Preact Signals & Bypassing the VDOM](https://codewithanimation.in/state-management/signals-bypassing-vdom.md)

## Computed & Effects

`computed()` derives a value from one or more base signals. It caches its output and only re-evaluates when its dependencies change, similar to `useMemo`.

`effect()` executes side effects automatically whenever any signal read inside its body updates, similar to `useEffect`.

## No More Dependency Arrays

React hooks require you to explicitly declare what dependencies you are tracking in a `[deps]` array. If you make a mistake, you introduce stale closures or infinite loops.

Signals track dependencies dynamically at runtime. If you read a `.value` anywhere inside a computed or effect closure, the signal registers it as a subscriber automatically!

## Lazy Evaluation

Unlike standard React renders, `computed()` signals evaluate lazily. They won't actually run their calculation until something (like an effect or a component) subscribes to them.

This means you can declare hundreds of complex computed derivations globally, and pay zero performance cost until they are actively rendered.

## Common mistake: The Stale Closure Bug

In standard React, a forgotten dependency in `useEffect` means the effect closure sees an old, stale version of a state variable.

Because Signals track reads dynamically as the JavaScript executes, the engine literally cannot suffer from stale closures. If the variable is read, the dependency is mapped.

## Tip: Global Scope

Because Signals don't depend on React's rendering lifecycle, you can define `effect()` blocks entirely outside of your components to run global background tasks.

## Try it: Concept Visual: Runtime Tracking

Watch how the Signals engine scans the code at runtime to automatically discover dependencies, eliminating the fragile React `[deps]` array.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/signals-computed-and-effects

## Check yourself

### Question 1

Why doesn't `effect()` require a dependency array like `useEffect`?

- A. Because `effect()` runs on every single browser frame.
- B. Because the Signals engine automatically tracks which `.value` properties are read during the function's execution.
- C. Because you pass the dependencies directly into the signal constructor instead.

Correct answer: B

Signals use dynamic runtime tracking. As the code executes, any `.value` getter that fires automatically registers the effect as a subscriber.

- Why not A: Signals are highly optimized; they only run when their tracked values change.
- Why not C: There is no dependency declaration at all. It's fully automatic.

## Remember this

- `computed()` derives and caches state lazily.
- `effect()` runs side effects reactively.
- Signals eliminate dependency arrays entirely.
- Runtime dependency tracking prevents stale closures by default.
