Not Another To-Do App: Part 7
Tags: richdata, webcomponents, lithtml, openwc
Published: 2019-09-06 16:00:15

#### Getting your hands dirty and feet wet with Open Web Component Recommendations...sort of.

> _This a cross-post of a Feb 26, 2019 article from [Medium](https://medium.com/@westbrook/not-another-to-do-app-169c14bb7ef9) that takes advantage of my recent decision to use Grammarly in my writing (so, small edits have been made here and there), thanks for looking again if you saw it there 🙇🏽‍♂️ and if this is your first time reading, welcome!_

_Welcome to “Not Another To-Do App”, an overly lengthy review of making one of the smallest applications every developer ends up writing at some point or another. If you’re here to read up on a specific technique to writing apps or have made your way from a previous installation, then likely you are in the right place and should read on! If not, it’s possible you want to [start from the beginning](https://dev.to/westbrook/not-another-to-do-app-2kj9) so you too can know [all of our characters’ backstories...](https://github.com/Westbrook/to-do-app)_

_If you’ve made it this far, why quit now?_

<hr/>

# Does Your Component Really Need to Know That?

![Does Your Component Really Need to Know That?](https://thepracticaldev.s3.amazonaws.com/i/9ciorrxxhravawi8wy38.jpeg)
<figcaption>Photo by <a href="https://unsplash.com/@cadop">Mathew Schwartz</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption>

It’s hard to say whether it comes from my training in Agile, or my learnings about Lean, or my training in agile (big A, little a, if you know...you know), or my own musings on the [MTU](https://medium.com/@westbrook/may-i-introduce-to-you-the-minimum-testable-unit-79a59772bcc4), but I’ve grown quite fond of not doing things that don’t have to be done. Along those lines, I often have overly long conversations with myself as to where I should position the control over a component; it’s content, it’s functionality, it’s styling, everything. I spent a good amount of time thinking about this particularly in relation to the implementation of my app’s `to-do` element.

Initially, in keeping with the least amount of work approach, I felt I could get away with comparing my to-dos with string-based equality. There was no data to define them beyond the text string of the to do, so I wanted to get away with code so simple as:

```html
<to-do todo="I've got stuff to do"></to-do>
```

A simple string meant I could rely on [attribute binding](https://lit-html.polymer-project.org/guide/writing-templates#bind-to-attributes) to push the necessary data into my `to-do` element, and I could call it a day. However, a quick self QA (quality assurance test) of this will show you the folly of this in a simple equality world:

```html
<to-do todo="I've got stuff to do"></to-do>
<to-do todo="I've got stuff to do"></to-do>
```

When you’ve got two to-dos of the same name, you also have two to-dos of the same equality, which means that “completing” one will _accidentally_ complete both. What’s a person with lots of to do with the same text to do?

First, I thought to do this:

```html
<to-do todo='{"todo": "I've got stuff to do"}'></to-do>
<to-do todo='{"todo": "I've got stuff to do"}'></to-do>
```

Thanks to the `LitElement` base class that open-wc’s Starter App supplies to build my web components with, I could declare my `todo` property as `{type: Object}` and I’d get the serialization of that attribute string into an actual `Object` for free. That object would then have a unique identity between my individual `to-do` elements and I could rely on equality checking again to “complete” one to do and not the other, and all was right with the world.

> ## Wrong

Relying on data that is being serialized across a component boundary internal to an application means that new identities are going to be created at times that are likely not the ones you meant. Particularly, when serializing into and out of a string via the binding outlined above, the external and internal identity of your objects will not be shared, which is how I had made my way to the following code:

```js
<to-do .todo="${todo}"></to-do>
```

Using property binding means that we can skip the requirement for string serialization and that not only will each `to-do` element have a `todo` with a unique identity, regardless of the work needed to display them, but that identity will also be maintained across component boundaries. Rich data communication in a web component? You don’t say...

With that decided, I spent a little time with the style application I hoped to achieve. I decided to invert the standard styling approach and rather than rely on `to-do` element internals (with the likely help of CSS custom properties) to style the to do text, I chose to apply it through the light DOM so that the parent element would have control of the styling. It’s a small difference, but it’s one less thing that my custom element has to think about.

```js
<to-do todo="${todo}">${todo.todo}</to-do>
```

What did I tell you, a small change! And, internally this change is paired with the addition of a `slot` element to display the content projected into your [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM) from outside. In this case, that looks like:

```js
render() {
    return html`
        <div>
            <slot></slot> <!-- <E<=<- Look, I'm a slot! -->
        </div>
        <button
            @click="${this.completeToDo}"
            title="Complete To Do"
        >
            ${iconMinus}
        </button>
    `;
}
```

Sometimes your components need to know more than you initially hope that they will need to, whether for maintaining their fidelity internally or for doing the same across component boundaries when constructing an application. Other times, you might be able to take some responsibilities off of the shoulders of your components. Getting philosophical and answering the question “Does Your Component Really Need to Know That?” can be an important step in both delivering the features you’re putting together now, as well as reducing maintenance requirements for later.

<hr/>

# The Short Game

As voted on by a plurality of people with opinions on such topics that are both forced to see my tweets in their Twitter feed and had a free minute this last week, a 9000+ word article is a no, no.

{% twitter 1098196160613896192 %}

So, it is with the deepest reverence to you my dear reader that I’ve broken the upcoming conversations into a measly ten sections. Congratulations, you’re nearing the end of the first! If you’ve enjoyed yourself so far, or are one of those people that give a new sitcom a couple of episodes to hit its stride, here’s a list of the others for you to put on your Netflix queue:

- [Not Another To-Do App](https://dev.to/westbrook/not-another-to-do-app-2kj9)
- [Getting Started](https://dev.to/westbrook/not-another-to-do-app-3jem) (Remember this, oh so long ago, we were but children then..)
- [Test Early, Test Often](https://dev.to/westbrook/not-another-to-do-app-2m9a)
- [Measure Twice, Lint Once](https://dev.to/westbrook/not-another-to-do-app-part-4-58cd)
- [Make it a Component](https://dev.to/westbrook/not-another-to-do-app-part-5-5d7o)
- [Make it a Reusable Part](https://dev.to/westbrook/not-another-to-do-app-part-6-an)
- Does Your Component Really Need to Know That? (you are here)
- [Some Abstractions Aren’t (Just) For Your App](https://dev.to/westbrook/not-another-to-do-app-part-9-10j3)
- [Reusable and Scaleable Data Management/And, in the end...](https://dev.to/westbrook/not-another-to-do-app-part-10-mp6)
- [See the app in action](https://gifted-lamport-70b774.netlify.com/)

<hr/>

Special thanks to the team at [Open Web Components](https://open-wc.org/) for the great set of tools and recommendations that they’ve been putting together to support the ever-growing community of engineers and companies bringing high-quality web components into the industry. [Visit them on GitHub](https://github.com/open-wc/open-wc) and create an issue, submit a PR, or fork a repo to get in on the action!