DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Algorithms: The Human Element in AI-Driven Cybersecurity
  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })
  • React Callback Refs: What They Are and How to Use Them

Trending

  • The Documentation Crisis Nobody Sees: Why AI Agents Are Breaking Faster Than Humans Can Document Them
  • Engineering Closed-Loop Graph-RAG Systems, Part 1: From Retrieval to Reasoning
  • Amazon OpenSearch Vector Search Explained for RAG Systems
  • Stop Loading Everything into Redshift: A Spectrum + Iceberg Pattern for Hybrid Analytics

Using Refs in Stencil

A discussion of why refs are important for front-end code, followed by a quick tutorial on using ref attributes with the Stencil framework.

By 
Gil Fink user avatar
Gil Fink
·
Aug. 17, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
10.2K Views

Join the DZone community and get the full member experience.

Join For Free

Why Use Refs?

The first question that you might ask yourself is why bother using element references? Let's clarify that.

In a typical data flow, you will pass props from the parent to it's child elements. When passing props, the child component will re-render and you will get it's new state. There are times that you will need to imperatively modify a child outside of this typical flow. Here are examples of things that might need different flow:

  • Triggering animation.
  • Usage of third-party components with their own API.
  • Implementation of forms where you might need to do things such as text selection or focus.

Now that we understand that we might need references to inner elements it's time to understand how to implement them in Stencil.

Stencil Refs

When you want to create a reference to an inner element in Stencil you will use the ref attribute. The ref attribute gets a function which accepts an element reference and returns void. In the function implementation, you will set the reference to a component member.

Let's see an example:

import {Component} from '@stencil/core';

@Component({
  tag: 'custom-text-input',
  shadow: true
})
export class CustomTextInput {
  private textInput?: HTMLInputElement;

  constructor() {
    this.focusTextInput = this.focusTextInput.bind(this);
  }

  focusTextInput() {
    this.textInput.focus();
  }

  render() {
    return (
      <div>
        <input
          type="text"
          ref={el => this.textInput = el as HTMLInputElement} />

        <input
          type="button"
          value="Focus the text input"
          onClick={this.focusTextInput}
        />
      </div>
    );
  }
}

In the example, you can see that the  CustomTextInput class has a private member called textInput. In the  render function, you set the reference to the input element using the following code:

ref={el => this.textInput = el as HTMLInputElement}

Then, when someone is clicking on the button, the focusTextInput is called and it will use the stored element to focus on the input element.

This is, of course, a simple example, but I hope that it helps you in setting a reference to an element.

Element

Published at DZone with permission of Gil Fink. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Algorithms: The Human Element in AI-Driven Cybersecurity
  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Playwright: Filter Visible Elements With locator.filter({ visible: true })
  • React Callback Refs: What They Are and How to Use Them

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook