Share logic across Svelte components (like React Hooks) with the use:directive

栏目: IT技术 · 发布时间: 5年前

内容简介:Write better components by utilizing one of the lesser-known but extremely powerful features of Svelte: actionsHappens to all of us, I guess. Definitely happened to me. And I don’t want it to happen to you. This is what this article is for.

Practical Svelte: The use-directive

Write better components by utilizing one of the lesser-known but extremely powerful features of Svelte: actions

Share logic across Svelte components (like React Hooks) with the use:directive

Jan 9 ·3min read

Share logic across Svelte components (like React Hooks) with the use:directive

Photo by NordWood Themes on Unsplash

Svelte is a great framework. It can feel like a breeze of fresh summer air. However, especially when starting out, it’s very easy to fall into the “this is how I did things in my previous framework” trap.

Happens to all of us, I guess. Definitely happened to me. And I don’t want it to happen to you. This is what this article is for.

Let’s say you want some of your input fields to automatically select all text on focus. Especially when coming from a framework like React, it might feel very tempting to start creating a component wrapper around the input (i.e. <input> becomes <Input/> ) and add your custom event handlers in there.

This is a great approach in React, but not so much in Svelte. Why, you ask?

Well, Svelte (at least in its current form, v3) really shines when you have native DOM elements at your disposal. You can use transition directives, conditionally switch CSS classes, bind to the current value with ease, and more.

And with a well-designed CSS styling system, you can go a very long way to consistently style the components the way you like while your code stays readable and maintainable.

But, alas, sometimes you want more. Sometimes, you may be tempted to write that wrapper. Because all your (React or Vue or insert your reactive framework here ) instincts tell you so.

Resist the temptation. There is a better way. A svelter way. Introducing: the use-directive (a.k.a. “actions”).

Although they are part of the standard tutorial, I didn’t use them for quite some time. Part of the problem may be that the example from the tutorial felt rather specific and did not really seem to apply to the usual problem sets.

But once I understood their full potential, I absolutely came to love them.

Here’s how it works: You take any native component (like an input, a button, or a div) and assign it a f unction that is executed on the DOM node of the component when the component is mounted .

Let’s take our use case from above (select the text on focus). Assuming we have a basic input, like so:

<input type="text" placeholder="Name" title="Name">

Step 1. We define a function that takes an HTML node (the node of the component on which the directive will be applied) and add our event listeners to the node.

selectTextOnFocus directive

Step 2. We assign this function to the component, using the “use-directive”.

<input type="text" placeholder="Name" title="Name use:selectTextOnFocus>

That’s it. Yes, it’s that simple. Now your input will select all the contained text whenever it receives focus.

One important part — which is also explained in the standard tutorial — is to make sure you run the necessary cleanup code when the component is destroyed (i.e. unmounted from the DOM).

This is what the return value {destroy:…} is for. Otherwise, you might introduce a memory leak to your app.

You assign more than one directive to an element. So, we could create a new function called blurOnEscape and assign it as well.

<input type="text" placeholder="Name" title="Name use:selectTextOnFocus use:blurOnEscape>

The code for this directive is again very straightforward:

blurOnEscape directive

Here are the examples from above in an interactive REPL .

The above examples are really basic, but the potential is huge. For example, at Faden, we wrote our multi-level drag and drop tree handling with a use directive, significantly slimming down the components itself.

Nevertheless, simple modular modifiers like the ones above often felt like the biggest productivity booster.

I would love to know your use cases. Also, let me know if you would like to see more advanced use cases, e.g. how to pass props to the directive and keep it in-sync with your component state.

And, since you read this until the end, here is one more example. This time in the interactive Svelte REPL: Listen for a click outside of an element and dispatch a custom event .

Very handy to use on floating elements of menus and dropdowns.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Linux程序设计

Linux程序设计

Neil Matthew、Richard Stones / 陈健、宋健建 / 人民邮电出版社 / 201005 / 99.00元

时至今日,Linux系统已经从一个个人作品发展为可以用于各种关键任务的成熟、高效和稳定的操作系统,因为具备跨平台、开源、支持众多应用软件和网络协议等优点,它得到了各大主流软硬件厂商的支持,也成为广大程序设计人员理想的开发平台。 本书是Linux程序设计领域的经典名著,以简单易懂、内容全面和示例丰富而受到广泛好评。中文版前两版出版后,在国内的Linux爱好者和程序员中也引起了强烈反响,这一热潮......一起来看看 《Linux程序设计》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器