diff --git a/guides/release/components/template-lifecycle-dom-and-modifiers.md b/guides/release/components/template-lifecycle-dom-and-modifiers.md index b8fb711cf4..a16fc06397 100644 --- a/guides/release/components/template-lifecycle-dom-and-modifiers.md +++ b/guides/release/components/template-lifecycle-dom-and-modifiers.md @@ -243,9 +243,9 @@ On the other hand, if you're looking at JavaScript documentation that tells you If you want to set a property, you can use the `prop` element modifier. -## Calling Methods On First Render +## Calling Methods On Render -So far, we've talked about web APIs that work by setting attributes as well as web APIs that work by setting properties. But what about web APIs that work by calling methods, like setting focus on an element? +So far, we've talked about web APIs that work by setting attributes as well as web APIs that work by setting properties. But what about web APIs that work by calling methods, like setting focus on an element? For example, let's say we want to focus an `` in a form as soon as the form is rendered. The web API for focusing an element is: @@ -253,35 +253,41 @@ For example, let's say we want to focus an `` in a form as soon as the fo inputElement.focus(); ``` -This code needs to run after the element is rendered. +We can use a modifier to run a method immediately after an element has rendered. +To follow along with the examples below, make sure you have `ember-modifier` installed in your app: -The simplest way to accomplish this is by using the `did-insert` modifier from [@ember/render-modifiers][render-modifiers]. - -[render-modifiers]: https://github.com/emberjs/ember-render-modifiers - -```handlebars {app/components/edit-form.hbs} -
- -
``` +ember install ember-modifier +``` + +Import `modifier` from `ember-modfier`, add an `autofocus` method to the component class, +and then use it in a component template. ```js {app/components/edit-form.js} import Component from '@glimmer/component'; -import { action } from '@ember/object'; +import { modifier } from 'ember-modifier'; export default class EditFormComponent extends Component { - @action - focus(element) { - element.focus(); - } + autofocus = modifier((element) => element.focus(), { eager: false }); } ``` -The `did-insert` modifier will call a function after its element is added to the DOM. That function receives the element as a parameter. +Here is how we can use our new modifier in a template: + +```handlebars {app/components/edit-form.hbs} +
+