Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 39 ยท Getting Started

Web Components and Frameworks

Compare Web Components with React, Vue and Angular. Learn what each does best, how they work together, and when a native component is the better choice.

Whenever Web Components come up, someone asks: "Do they replace React?" The honest answer is that they solve a different, smaller problem. A framework gives you an entire application model: state, rendering, routing, tooling. Web Components give you one thing very well: a reusable, encapsulated element that the browser understands. Many teams use both, and this lesson helps you decide.

What each side offers

Concern Web Components Typical framework
Defining a reusable tag Built in Component function or class
Style isolation Shadow DOM (built in) CSS modules, scoped styles, conventions
Reactive rendering You write it (or add a tiny library) Core feature
Data binding in templates None built in Core feature
State management None Built in or ecosystem
Routing, forms, data fetching None Ecosystem
Works without a build step Yes Usually needs a build tool
Works outside the framework Anywhere HTML works Inside that framework
Lifespan As long as the web platform Depends on the project

The table shows the trade. Web Components are small and portable but leave rendering to you. Frameworks are productive for whole apps but tie the component to the framework.

Rendering by hand is the cost

Without a framework, updating the UI when data changes is your job. Here is a counter written with explicit DOM updates. It is fine for small elements, and it makes clear what a framework automates.

Manual rendering

Notice #render(). Every state change must call it. In a framework you would change the data and the library would work out what to update. Small helper libraries (such as Lit, covered in the tooling chapter) give Web Components that same convenience while still producing standard custom elements.

Using Web Components inside frameworks

Because a custom element is just an HTML tag, every framework can render it. A few details are worth knowing.

  • Attributes versus properties. Attributes are always strings. Frameworks differ in whether they set a property or an attribute on a custom element, so a component should accept both for important values (see the chapter on events and properties).
  • Events. Components communicate by dispatching DOM events. Frameworks listen with their usual syntax; older versions of some frameworks needed a small wrapper for custom event names.
  • Slots. Children written inside the tag become slotted content, which works as the equivalent of "children" or "slots" in framework terms.

The following read-only snippet shows the same tag in three environments. It is not runnable here because it needs each framework's build setup.

HTML
<!-- Plain HTML -->
<user-chip name="Ada"></user-chip>
JSX
// React (read-only: needs a build step)
function Profile() {
  return <user-chip name="Ada" />;
}
HTML
<!-- Vue template (read-only) -->
<user-chip :name="user.name"></user-chip>

The element is identical; only the surrounding syntax changes. That is the portability argument.

Using frameworks inside Web Components

The reverse also works. A custom element can render its internals with any library: it creates a shadow root, mounts the library into it, and exposes a clean tag outside. Several frameworks include a "compile to custom element" mode for this purpose. The result is a framework-powered widget that other teams can drop into any page.

When Web Components are a great fit

  • Design systems shared by teams using different frameworks.
  • Embeddable widgets (chat, comments, payment, maps) that must not clash with the host page's CSS.
  • Content sites that need small interactive pieces without shipping a framework runtime.
  • Long-lived components where you do not want to rewrite when a framework falls out of fashion.
  • Micro-frontends where independent teams own parts of one page.

When a framework is the better choice

  • A large single-page application with routing, global state and heavy data flow.
  • A team already productive in one framework that does not need to share components outside it.
  • Projects needing server-side rendering with hydration today, where a framework's tooling is more mature than hand-rolled declarative Shadow DOM.

You do not have to choose forever. A common path is to build leaf components (buttons, dialogs, inputs) as Web Components and keep application logic in whatever framework the team prefers.

A decision checklist

flowchart TD
  A["Need a reusable UI piece?"] -->|Used in one app only| B["Use the app's framework component"]
  A -->|Shared across apps or teams| C["Web Component"]
  C --> D["Need reactive rendering?"]
  D -->|Small helper is enough| E["Add a tiny library such as Lit"]
  D -->|Simple static output| F["Plain custom element"]

Reuse across boundaries is the strongest reason to choose a Web Component.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main thing Web Components do NOT include out of the box?

    The platform offers tags, encapsulation and lifecycle, but not a declarative reactive rendering system.
  2. 2. Why are Web Components popular for design systems?

    A custom element is a standard tag, so teams on different frameworks can all use the same component.
  3. 3. How do custom elements usually tell the outside world something happened?

    Components dispatch events such as CustomEvent, which any listener or framework can handle.
  4. 4. Which situation favors a full framework over plain Web Components?

    Large applications benefit from the routing, state and tooling that frameworks provide.

Summary

  • Web Components solve reuse and encapsulation; frameworks solve whole-application structure.
  • Manual DOM updates are the price of using the platform alone, and tiny libraries reduce it.
  • Custom elements work inside every framework, and frameworks can power elements from the inside.
  • Choose Web Components for shared, embeddable and long-lived pieces.
  • Combining the two is common and healthy.

Next: Setting Up and Debugging Web Components