0Pricing
React Academy · Lesson

When to Use Imperative vs Declarative APIs

Understand the trade-offs and decide when reaching for refs and imperative handles is justified.

When to Use Imperative vs Declarative APIs is a free React Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

React Is Declarative by Default

React encourages a declarative style: you describe what the UI should look like for a given state, and React reconciles the DOM to match. Most behavior flows through props and state rather than direct DOM commands.

This model makes UIs predictable, since the rendered output is a pure function of state rather than the result of a sequence of manual mutations.

When Declarative Is Insufficient

Some interactions resist a purely declarative expression. Imperatively focusing an element, triggering an animation at a precise moment, or driving a third-party DOM widget often cannot be captured cleanly as a function of state.

For these cases React provides escape hatches like refs and imperative handles, used sparingly where declarative props fall short.

The Cost of Imperative Code

Imperative APIs come with tradeoffs. They are harder to test, since you must drive methods and observe side effects, and they can break the unidirectional data flow that makes React apps easy to follow.

Imperative calls also tend to scatter behavior across event handlers rather than concentrating it in a clear state-to-UI mapping, raising maintenance cost.

The Rule of Thumb

Prefer declarative APIs and reach for imperative ones only for interactions and measurements: focus, scroll, animations, media playback, and reading sizes or positions.

If something can be modeled as state that maps to rendered output, do that. If it is a momentary action with no clean state representation, an imperative handle is appropriate.

Declarative vs Imperative Modal

A declarative modal renders based on an isOpen prop the parent owns, so visibility is driven by state. An imperative modal exposes open and close methods, hiding its own state behind a handle.

The declarative version fits React data flow better and is usually preferred, while the imperative version can be convenient when triggering must happen from scattered, event-driven code.

Form Reset: key Prop vs Imperative Reset

You can reset a form declaratively by changing its key prop, which forces React to remount it fresh with default state. No imperative call is required.

The imperative alternative exposes a reset method on a handle. The key trick is often cleaner because it leans on React reconciliation rather than manual state clearing.

Router Prefetch as an Imperative Example

The Next.js router exposes imperative methods like push, replace, and prefetch. Prefetching a route ahead of a likely navigation is an action, not a piece of render state, so an imperative call fits naturally.

This shows that even idiomatic React tooling uses imperative APIs where the operation is fundamentally an action triggered at a moment in time.

Refs Are Escape Hatches

The official guidance frames refs and imperative handles as escape hatches, tools for stepping outside the declarative model when you genuinely must. They are not the default way to build features.

Keeping this mindset prevents overuse: you reach for an escape hatch only after confirming that props and state cannot express the behavior cleanly.

A Summary Checklist

Before adding an imperative handle, ask: can this be state mapped to UI? If yes, stay declarative. Is it a focus, scroll, measurement, animation, or third-party action? If yes, imperative may be justified.

Also confirm the surface is small and documented, and that you are not using a ref simply to pass data a prop could carry.

Balancing Both Styles

Real applications blend both approaches. The vast majority of logic stays declarative, with a thin layer of imperative handles for the handful of interactions that need them.

Striking this balance keeps your code predictable and testable while still letting you handle focus management, media control, and library integration where declarative code cannot reach.

Quick Check: Declarative vs Imperative

Choose when an imperative API is the right tool.

Recap: Declarative vs Imperative

React defaults to declarative UI driven by props and state, which is predictable and testable. Imperative handles exist as escape hatches for focus, scroll, animation, measurement, and third-party DOM control.

Compare declarative and imperative modals, prefer the key prop for form resets, and recall that even the Next.js router uses imperative methods for actions. Keep the imperative layer small, documented, and justified.

Frequently asked questions

Is the “When to Use Imperative vs Declarative APIs” lesson free?

Yes — the full text of “When to Use Imperative vs Declarative APIs” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “When to Use Imperative vs Declarative APIs”?

Understand the trade-offs and decide when reaching for refs and imperative handles is justified. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “When to Use Imperative vs Declarative APIs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. forwardRef: Exposing DOM Refs to Parents
  2. useImperativeHandle: Custom Instance Values
  3. Building an Imperative Component API
  4. When to Use Imperative vs Declarative APIs
← Back to React Academy