<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Marcus Stamström</title>
    <description>The latest articles on DEV Community by Marcus Stamström (@mstamstrom).</description>
    <link>https://dev.to/mstamstrom</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F141712%2Fe198b455-7570-46af-8a08-7818869a3fef.jpeg</url>
      <title>DEV Community: Marcus Stamström</title>
      <link>https://dev.to/mstamstrom</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mstamstrom"/>
    <language>en</language>
    <item>
      <title>Building custom HTML elements with web components</title>
      <dc:creator>Marcus Stamström</dc:creator>
      <pubDate>Sun, 29 Nov 2020 18:38:11 +0000</pubDate>
      <link>https://dev.to/mstamstrom/building-custom-html-elements-with-web-components-4lb</link>
      <guid>https://dev.to/mstamstrom/building-custom-html-elements-with-web-components-4lb</guid>
      <description>&lt;h3&gt;
  
  
  Imagine building reusable, encapsulated HTML elements, without any framework! With web components, we can certainly create it!
&lt;/h3&gt;

&lt;h2&gt;
  
  
  What are Web components?
&lt;/h2&gt;

&lt;p&gt;Web components are a set of web platform APIs that allows to create custom, reusable and encapsulated HTML elements that can be used in any document or web app. Web components consist of 3 different technologies.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Custom elements&lt;/strong&gt;, a set of Javascript APIs that permits to create custom HTML elements and define their behavior.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shadow DOM&lt;/strong&gt;, is used to encapsulate javascript and styling to specific components.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTML templates&lt;/strong&gt;, is used to declare markup that goes unused at page load, but it can be instantiated later at runtime.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Custom elements
&lt;/h2&gt;

&lt;p&gt;Custom elements are the foundation of web components. With the &lt;code&gt;CustomElemets&lt;/code&gt; API, we are creating new HTML elements based on desired behavior and logic. &lt;/p&gt;

&lt;p&gt;Constructing a custom element is similar to construct a component in Vue, React or insert-flavor-of-the-week framework, but without the need for a framework. These custom elements can be used in any document, both directly in HTML or in web applications. Another advantage of using web components, since these custom elements are supported by browsers, is that they don't become obsolete (in contrast to SPA framework).&lt;/p&gt;

&lt;p&gt;In order to build a custom element, a javascript class is necessary to extend &lt;code&gt;HTMLElement&lt;/code&gt; and define the class with a tag name. A fundamental version of a custom element:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/marcusalfredsson/embed/WNxqEGB?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Despite the fact that this example isn't advanced, it allows being used as a starting block. The example illustrates that the javascript class extends &lt;code&gt;HTMLElement&lt;/code&gt; allowing the component to be registered by the browser. It's important to define the custom HTML element with a tag in order to use it from the document. Defining a tag is achieved with &lt;code&gt;customElements.define('hello-world', HelloWorld)&lt;/code&gt;. After fulfilling these steps, we have built a custom element, ready to be used in any document by simply writing &lt;code&gt;&amp;lt;hello-world&amp;gt;&amp;lt;/hello-world&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Additionally, extending &lt;code&gt;HTMLElement&lt;/code&gt; allows access to the API used by HTML elements, for example, lifecycle events. In the exposed case, lifecycle event &lt;code&gt;connectedCallback&lt;/code&gt; is executed when the component is inserted into the DOM. &lt;code&gt;connectedCallback&lt;/code&gt; is the right location for adding initial content to elements or fetching data to be rendered in a component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: A custom element always has a dash in the tag name, for example &lt;code&gt;my-component&lt;/code&gt;, &lt;code&gt;hello-world&lt;/code&gt; or &lt;code&gt;whats-up&lt;/code&gt;. Browser vendors have bound not to use dashes in HTML tags, to avoid conflicts in tag name.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML template
&lt;/h2&gt;

&lt;p&gt;With HTML templates, we define HTML that will be instantiated later at runtime.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello world&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code snippet above renders an empty page as a result of the template's content aren't displayed in the browser. With this powerful technique, we can define and store HTML in the DOM and display the content when desired. To display the content of a template we need to use javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;template&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;importNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start with fetching the template from the DOM, next copy the template to the &lt;code&gt;node&lt;/code&gt; variable using &lt;code&gt;importNode&lt;/code&gt; and at the end insert the newly created &lt;code&gt;node&lt;/code&gt; into the DOM. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;importNode&lt;/code&gt; copies the template's content and in consequence it can be reused in several places for a document. After being executed, the DOM is similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello world&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Hello world&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A great aspect about templates is the possibility to include any HTML, style or scripts. As a result templates are a good location for styling our component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;template&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"counter"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Click me&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"times"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;0&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Shadow DOM
&lt;/h2&gt;

&lt;p&gt;The DOM (Document Object Model) represents the structure of the HTML document. The DOM has a tree structure that models a document with a parent-children relationship.&lt;/p&gt;

&lt;p&gt;The DOM API has absent support for encapsulation. This characteristic makes it difficult to create reusable, encapsulated custom elements.&lt;/p&gt;

&lt;p&gt;Encapsulation is however possible in the shadow DOM and is accessible to use javascript and styling to custom elements. When creating a shadow DOM, a subtree is attached to one DOM element. The newly created shadow DOM subtree is encapsulated from the rest of the document and our shadow DOM subtree cannot affect the residual part of the document.&lt;/p&gt;

&lt;p&gt;Aside from encapsulation, the API for the DOM and the shadow DOM work similarly. Furthermore functions like &lt;code&gt;querySelector&lt;/code&gt;, &lt;code&gt;textContent&lt;/code&gt;, &lt;code&gt;getElementById&lt;/code&gt; etc. from the API can still be used.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/marcusalfredsson/embed/YzWobNg?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;This exemplifies how shadow DOM is attached to the root of a custom element with &lt;code&gt;this.attachShadow({mode: 'open'})&lt;/code&gt;. Now the generated shadow DOM will encapsulate javascript, HTML and styling inside the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;template&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"counter"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Click me&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"times"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;0&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;my-counter&amp;gt;&lt;/span&gt;
  #shadow-root
    &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Click me&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"times"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;0&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/my-counter&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After we render our component the final result of the DOM is outlined as in the present example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Without using any SPA framework, we are able to create encapsulated, reusable web components that are a great benefit to worldwide users. &lt;/p&gt;

&lt;p&gt;Considering that web development becomes increasingly complex, it's reasonable to invest more development in the web platform. From this perspective, I believe web components are a great complement to SPA frameworks such as Vue and React. They don't substitute each other, but it's very suitable to build these custom HTML elements without any framework.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>webcomponents</category>
      <category>javascript</category>
    </item>
    <item>
      <title>SvelteJS: The next big UI framework</title>
      <dc:creator>Marcus Stamström</dc:creator>
      <pubDate>Sun, 29 Sep 2019 14:10:52 +0000</pubDate>
      <link>https://dev.to/mstamstrom/sveltejs-the-next-big-ui-framework-4n0e</link>
      <guid>https://dev.to/mstamstrom/sveltejs-the-next-big-ui-framework-4n0e</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fhoe8sm40up1ytcqkfqzu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fhoe8sm40up1ytcqkfqzu.png" alt="SvelteJS header picture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  SvelteJS: The next big UI framework
&lt;/h1&gt;

&lt;p&gt;SvelteJS is the new UI framework on the block. Svelte is however very different in many aspects and especially in its mindset around how a UI framework should work and how it should solve the problems regarding writing a user interface. In this post, we will explore how to get started with SvelteJS by building a todo app (as always 😃 ). We will at the same time learn about what Svelte brings to the table and how it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL:DR
&lt;/h2&gt;

&lt;p&gt;SvelteJS is a compiler UI framework, some of its features are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Truly reactive framework&lt;/li&gt;
&lt;li&gt;Easy to learn&lt;/li&gt;
&lt;li&gt;Accessibility support&lt;/li&gt;
&lt;li&gt;Super fast and small bundle sizes&lt;/li&gt;
&lt;li&gt;Scoped CSS&lt;/li&gt;
&lt;li&gt;Fullfledge framework with a big toolbox to help develop faster.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stay around and we will explore all this and more in this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Svelte?
&lt;/h2&gt;

&lt;p&gt;Svelte is a framework for building user interfaces, like Vue or React. The key difference is that Svelte is a compiler, unlike React or Vue which runs in the browser. This key difference together with that Svelte is truly a reactive framework (which I would argue that React and Vue are not) opens a lot of opportunities that we will explore in this post.&lt;/p&gt;

&lt;p&gt;In Svelte, we write code in the same declarative way as we do in for example React or VueJS. We do really notice that the UI framework is a compiler, which we also will see in the examples later on. &lt;/p&gt;

&lt;h2&gt;
  
  
  How do we get started?
&lt;/h2&gt;

&lt;p&gt;The easiest way to get started is to download a boilerplate template from npx or start a project in codesandbox. To create a boilerplate template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx degit sveltejs/template my-todo-app
&lt;span class="nb"&gt;cd &lt;/span&gt;my-todo-app/

npm &lt;span class="nb"&gt;install
&lt;/span&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy as cake. Now we have a SvelteJS setup ready and can start coding. This project is setup with rollup, which is a bundler, like webpack but more minimalistic.&lt;/p&gt;

&lt;h2&gt;
  
  
  The boilerplate template with SvelteJS
&lt;/h2&gt;

&lt;p&gt;At first glance, the project structure looks quite similar to what you would get from React or Vue from scratch.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fre0bu6m160zdkaj4quyg.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fre0bu6m160zdkaj4quyg.PNG" alt="Svelte project structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that we have a &lt;code&gt;main.js&lt;/code&gt; file, which basically does the same as for other UI frameworks, that is injecting the created js bundle into an HTML element. That's all about the setup, let us check out the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Svelte components
&lt;/h2&gt;

&lt;p&gt;SvelteJS components are similar to HTML files, but with a lot of sugar on top. Javascript is typed in a script tag and CSS is typed in a style tag. The rest is interpreted as HTML. Here is the App.svelte component which comes with the template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;purple&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello {name}!&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It prints the name variable which is passed in from main.js. So that’s the first thing we can see that is different syntactically. &lt;strong&gt;Props are those properties that are exported in the script tag.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reactivity by assignment
&lt;/h2&gt;

&lt;p&gt;But we want to create a todo app so let's start making changes. Let us start by adding an input field and connect that with a variable&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flb8yrchfhmsxx39tqxhd.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flb8yrchfhmsxx39tqxhd.PNG" alt="First code example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty similar to React or Vue, but with a lot less boilerplate. We have a todo variable which gets updated when the value in the input field is changed. &lt;/p&gt;

&lt;p&gt;Notice that we are using the assignment operator. &lt;strong&gt;The assignment operator is a big part of SvelteJS reactivity system&lt;/strong&gt;. When assigning a new value to a variable, that assignment will also tell SvelteJS that something has changed in the App and that Svelte needs to update the variable in the DOM in the end of the event loop. Yes, there is an event loop to batch DOM updates, which is good for minimizing repaints.&lt;/p&gt;

&lt;p&gt;We can actually make this a little cleaner with the help of the SvelteJS two-way data binding directive.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdvp92jsyez8x5qzgufue.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdvp92jsyez8x5qzgufue.PNG" alt="Second code example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And it still works like before. We are using the &lt;code&gt;bind&lt;/code&gt; directive to use both read and write operation for HTML elements. This also works for checkboxes and other kinds of inputs.&lt;/p&gt;

&lt;p&gt;Let's continue coding and check how to iterate over arrays.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F591nft3afuudd7dplmzs.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F591nft3afuudd7dplmzs.PNG" alt="Third code example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have added the possibility to add todos to an array and display those added todos. There are some important observations in the newly added code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;the &lt;code&gt;${each}&lt;/code&gt; command in the HTML iterates over an array, with the item as its first parameter and the index as its second parameter. Notice that we have to close the iteration with &lt;code&gt;{/each}&lt;/code&gt;. To use the index write &lt;code&gt;{#each todos as (todo, index)}&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;on line 5 we reassign the array instead of using push, &lt;strong&gt;in SvelteJS, using operations like push will not trigger an update of that value&lt;/strong&gt;. This is a very important observation, as we mentioned previously SvelteJS reactivity system is built around the use of the assignment operator. So we cant use operations that don't return the updated variable. Push returns the length of the array after adding a new item.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In React, VueJs and other frameworks we need to have a wrapping element for each component. In SvelteJS &lt;strong&gt;we don't need a wrapping element for each component&lt;/strong&gt;, which helps avoid div nesting issues.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is no &lt;code&gt;this&lt;/code&gt; in the component. Since SvelteJS is a compiler, it doesn't have to apply all the rules that UI frameworks that run in the browser have to.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  But we are not completely reactive yet
&lt;/h2&gt;

&lt;p&gt;Now we have seen some examples of how reactivity works in SvelteJS. It works a bit different since the code is compiled at compile time and only run once in the browser. We have seen how we can have variables in the HTML and if we assign a new value to that variable, the DOM also updates. But what if we have a variable that depends on another variable. Let's consider this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isDone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;infoMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;isDone&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You finished&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Keep going!!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we would change the isDone variable somewhere in the code, that would not recompute infoMessage. As we just mentioned, that is because the code only run once. Let's take another example with our todo app, which now has the possibility to set todos as done as well as filtering on remaining todos.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fz6nzzue3dvugqzre250h.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fz6nzzue3dvugqzre250h.PNG" alt="Fourth code example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see in this example, it's not reactive. I click the checkbox to hide buy groceries, but it's not disappearing. That's because our code only run once.&lt;/p&gt;

&lt;p&gt;Svelte has come up with a solution for this problem, by "adding" a reactive command. We can add a &lt;code&gt;$:&lt;/code&gt;, which is valid javascript and is a labeled statement. It doesn't really do anything so Svelte uses this for reactivity. How that work is that the &lt;code&gt;$:&lt;/code&gt; is like a subscription and subscribes to the values in the expression. So if we revisit our example and do this change, then it should work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbifhl8ny8t4xbljnkvgn.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbifhl8ny8t4xbljnkvgn.PNG" alt="Fifth code example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this change, our todo app is now reactive and filters out the done todos when selecting the checkbox.&lt;/p&gt;

&lt;p&gt;How that works more exactly is that this code runs in topological order. Which means that &lt;code&gt;filteredTodos&lt;/code&gt; depend on &lt;code&gt;onlyShowRemainingTodos&lt;/code&gt; and &lt;code&gt;filtreredTodos&lt;/code&gt; will run after &lt;code&gt;onlyShowRemainingTodos&lt;/code&gt; has changed. This means we can also have reactive values that depend on other reactive values. We could, for example, have a variable that depends on filtered todos:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Buy groceries&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isDone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Go to the Gym&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;isDone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;}];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;onlyShowRemainingTodos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nl"&gt;$&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;filteredTodos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;onlyShowRemainingTodos&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt;
  &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isDone&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nl"&gt;$&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;numberOfFilteredTodos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;filteredTodos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Scoped CSS
&lt;/h2&gt;

&lt;p&gt;All CSS in the style tag is by default scoped, which is really awesome. This means we don't have to worry about complicated naming schemes like BEM or using pre-processors. You could still use BEM or pre-processors if you want to, but there aren't as many benefits when the global CSS issues are already solved. What scoped CSS gives us, is that the CSS is specific for each component. So we can have the same CSS class in 2 different components without having name collision.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6b8blnb2y1xzmqcyg94v.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6b8blnb2y1xzmqcyg94v.PNG" alt="Example scoped CSS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see in this example, svelte hashes our CSS to be specific for the App component, thereby making it scoped. Also notice that I added a CSS class that is not used in the HTML. That CSS class will not be included by Svelte in the CSS bundle, since it's not used anywhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other key features
&lt;/h2&gt;

&lt;p&gt;So far we have covered some of the features in SvelteJS, there are however a lot more. We will touch on some more features briefly, but we won't go through all of them in this article. For complete code example and to see all the features, I encourage you to visit &lt;a href="http://svelte.dev" rel="noopener noreferrer"&gt;svelte.dev&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;p&gt;In the compile process, Svelte compiles our svelte components into high-performance imperative javascript code. This makes our code very fast to run for the browser. As I mentioned, our code only runs once and not every time some state changes, which is a huge performance benefit. When Svelte turns our declarative code into high-performance imperative code, it also disappears from our bundle, since all the logic for updating the DOM is done in the build process and what's left of SvelteJS in our bundle is just some helper functions.&lt;/p&gt;

&lt;p&gt;Okay, so we have small bundles which mean faster loading time and faster time to first interactivity and our app is faster due to the imperative code. What not to like 😃&lt;/p&gt;

&lt;h3&gt;
  
  
  No virtual DOM
&lt;/h3&gt;

&lt;p&gt;With Svelte, all the computation for working out the most efficient way of reacting to a state change is done beforehand. With this way of thinking, we don't need a virtual DOM. The virtual DOM can now actually be seen as a bottleneck for performance. Svelte does not use a virtual DOM and is much faster because of it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Easy to learn
&lt;/h3&gt;

&lt;p&gt;Unlike most other frameworks, Svelte components look like HTML with javascript and CSS put in script and style tags. This makes the code more familiar to those not experienced with any UI frameworks. For those who are already using UI frameworks, its very easy to get started as well.&lt;/p&gt;

&lt;p&gt;There is also very little boilerplate, which makes it very easy to read and understand. This is a big selling point when learning a new framework, it lets you do what you are supposed to do, without typing much framework specific code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fullfledge framework
&lt;/h3&gt;

&lt;p&gt;That SvelteJS is a compiler also gives us an advantage when it comes to which features that Svelte can include in the framework. It gives Svelte the advantage of including a lot of features, but its &lt;strong&gt;only the features that you use in the app that will be included in the bundle&lt;/strong&gt;. The rest of the feature will be tree shaken away. That is really great, it gives us a very big toolbox to choose from, making development easier, without having to pay for the features we aren't using.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I think Svelte is a really great framework. It's easy to learn, fast and has a lot of usable and cool features. It gives a different mindset about how to think about UI frameworks in terms of reactivity and what a UI framework should help to solve. It feels easy to start working with Svelte because of their big toolbox and we don't have to worry as much about performance. I think Svelte will become one of the big UI frameworks in the future and I hope I have inspired you to give it a try, either by following my steps and setting up your own project or go &lt;a href="http://svelte.dev" rel="noopener noreferrer"&gt;svelte.dev&lt;/a&gt; and try some of the tutorial steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading !!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>svelte</category>
      <category>frameworks</category>
    </item>
    <item>
      <title>Coding with a Vue</title>
      <dc:creator>Marcus Stamström</dc:creator>
      <pubDate>Fri, 31 May 2019 08:40:45 +0000</pubDate>
      <link>https://dev.to/mstamstrom/coding-with-a-vue-ni8</link>
      <guid>https://dev.to/mstamstrom/coding-with-a-vue-ni8</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IaLjwtIE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://upload.wikimedia.org/wikipedia/commons/f/f1/Vue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IaLjwtIE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://upload.wikimedia.org/wikipedia/commons/f/f1/Vue.png" width="400" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Vue is this cool, popular and easy to learn UI framework. All you need to know to get started is HTML, CSS, and Javascript. So let's dig in and get started with learning how Vue works and building an application with VueJS. If you already know Vue and want to jump straight to the exercises, here is the &lt;a href="https://github.com/mStamstrom/vue-weather-app-exercises" rel="noopener noreferrer"&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An introduction
&lt;/h2&gt;

&lt;p&gt;Vue is a progressive framework for building user interfaces. Progressive in this cases meaning that Vue both can exist in an existing solution as well as powering the whole website thanks to its rich ecosystem. Vue is focused on UI updates and leaves parts like routing and global state management out of the framework, but is easy to include if necessary.&lt;/p&gt;

&lt;p&gt;When building an app in Vue, each page is split up into small reusable components that can be shared between components and pages.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3bi3277np64j28swncw4.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3bi3277np64j28swncw4.PNG" alt="https://vuejs.org/v2/guide/index.html#Composing-with-Components" width="660" height="256"&gt;&lt;/a&gt;&lt;br&gt;
When splitting up logic into smaller components, the code base becomes more manageable and also more testable. Each component has its own state, so if we would reuse a component in several places, changes to one component would not affect the others.&lt;/p&gt;

&lt;p&gt;Vue utilizes a virtual DOM and can thereby decide when to best update the DOM and also perform asynchronous DOM updates. Which gives fast and optimized UI updates.&lt;/p&gt;

&lt;p&gt;At the core of Vue.js, it's a system that enables us to declaratively render data to the DOM using straightforward template syntax. Consider the following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    {{ message }}
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;el&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello Vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  Hello Vue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have created our first app, even though its a very simple app. Vue does, however, do a lot under the hood and have now linked the DOM with our component data. Hence when changing our data, the view will also change, making Vue components reactive.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTML, CSS and Javascript
&lt;/h3&gt;

&lt;p&gt;The only requirements for learning Vue is HTML, CSS, and Javascript. This is also what each component consists of, split up into different parts. Each Vue component consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;script tag, defining Javascript&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;template tag, defining HTML&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;style tag, defining CSS&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, this is a component that prints Hello Vue!, and as we can see nothing more than HTML, CSS, and Javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello Vue!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/template&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt; &lt;span class="nx"&gt;scoped&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;greeen&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/style&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an exportable component that can be used by any other component. Note the &lt;code&gt;&amp;lt;style scoped&amp;gt;&lt;/code&gt;, this is something that is included in Vue and makes the style scoped to the current component, leaving no other classes affected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Vue instances
&lt;/h2&gt;

&lt;p&gt;Every Vue application starts by creating a Vue instance with the function &lt;code&gt;Vue&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;render&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;h&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;$mount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This usually looks something like this in the &lt;code&gt;main.js&lt;/code&gt; file. Here we create our Vue instance and tell Vue that the root component is App, which is imported in &lt;code&gt;main.js&lt;/code&gt;. Then Vue will create a component tree of the App component and all its subcomponents and so on. When Vue has created the component tree and calculated its initial state, it will insert the component tree on the &lt;code&gt;#app&lt;/code&gt; element, usually a div element somewhere in the root HTML file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data
&lt;/h3&gt;

&lt;p&gt;If we consider the root Vue instance, this instance needs to know when to perform an update on components in the component tree. This is where the data property comes in. The data property tells Vue which attributes that should trigger a rerender of that component.&lt;/p&gt;

&lt;p&gt;How that works is that when Vue creates the component tree, it checks all the attributes in all the components data properties and creates getters and setters for each attribute. When one of these data attributes change, Vue will receive an event and can thereby trigger a rerender.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in the above example, when the message changes, Vue will trigger a rerender of this component. Attributes in the data property are accessed directly under this, so in this case, &lt;code&gt;message&lt;/code&gt; could be altered with &lt;code&gt;this.message&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Methods
&lt;/h3&gt;

&lt;p&gt;Methods are where we usually put logic regarding state changes of a component. Consider the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;clicks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;}),&lt;/span&gt;
      &lt;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clicks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;clicks&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;click&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;onClick&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;You&lt;/span&gt; &lt;span class="nx"&gt;clicked&lt;/span&gt; &lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="nx"&gt;clicks&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt; &lt;span class="nx"&gt;times&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/template&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This simple component counts each click. When we click, we call the onClick method that updates the clicks data attribute of this component. When the clicks data variable updates, Vue will notice and perform a rerender of this component, then displaying the correct value of the state of this component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Template syntax
&lt;/h2&gt;

&lt;p&gt;Vue uses an HTML like templating syntax, which is powerful and removes most of the need for writing Javascript in the template. In the template, we write HTML, with some additional Vue directives and declaratively bind the rendered DOM elements with the Vue instance data.&lt;/p&gt;

&lt;p&gt;The most basic type of data binding is the double brackets, to print data to the DOM&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    {{ message }}
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Data binding
&lt;/h3&gt;

&lt;p&gt;When we want to bind a certain piece of data to a component or element declaration in the template, we use the v-on directive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;v-on:title=&lt;/span&gt;&lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    {{ message }}
  &lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;v-on&lt;/code&gt; tells that &lt;code&gt;title&lt;/code&gt; is a javascript element, which should be located in the script tag of the component. The &lt;code&gt;v-on&lt;/code&gt; has a shorthand that is mostly used, &lt;code&gt;:&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;:title=&lt;/span&gt;&lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    {{ message }}
  &lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Events
&lt;/h3&gt;

&lt;p&gt;When we want to listen to a DOM event, like click, we listen to this with the &lt;code&gt;v-on&lt;/code&gt; vue directive as well. Vue has a different shorthand for events, &lt;code&gt;@&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;v-on:click=&lt;/span&gt;&lt;span class="s"&gt;"actionOnClick"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    click me
  &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

  &lt;span class="c"&gt;&amp;lt;!-- shorthand --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;click=&lt;/span&gt;&lt;span class="s"&gt;"actionOnClick"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    click me
  &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  v-if vs v-show
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;v-if&lt;/code&gt; and &lt;code&gt;v-show&lt;/code&gt; are 2 different ways of deciding if elements should be shown in the UI. They have a key difference in that &lt;code&gt;v-if&lt;/code&gt; removes the element from the DOM when false, while &lt;code&gt;v-show&lt;/code&gt; set &lt;code&gt;display:none&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-if=&lt;/span&gt;&lt;span class="s"&gt;"show"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"show"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  v-for
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;v-for&lt;/code&gt; is used when iterating over elements in the template. Keys "must" be given, since its the key the Vue binds to the DOM to the element. Keys must be unique for that element and providing a nonunique key will result in faulty updates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"item in items"&lt;/span&gt; &lt;span class="na"&gt;:key=&lt;/span&gt;&lt;span class="s"&gt;"item.id"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    {{ item.name }}
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dont do this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"(item, index) in items"&lt;/span&gt; &lt;span class="na"&gt;:key=&lt;/span&gt;&lt;span class="s"&gt;"index"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    {{ item.name }}
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the index is not specific for the item but for the iteration, if the elements in the items are would change place, like when sorting or filtering, wrong elements would update.&lt;/p&gt;

&lt;h2&gt;
  
  
  Component communication
&lt;/h2&gt;

&lt;p&gt;A page in a Vue application is built up of many small components in a component tree as we saw in the components section. Quite often we want to communicate between components in the component tree. There are 2 ways of communication, up and down. When we communicate down we send data down to the child components, this will in the child component be visible as props. When a child component wants to communicate to the parent component they emit an event.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhid121tzx4x5ibcjb6ot.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhid121tzx4x5ibcjb6ot.png" alt="https://medium.com/@sky790312/about-vue-2-parent-to-child-props-af3b5bb59829" width="790" height="646"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's also explain by example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;animals&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;animalList&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;horse&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="na"&gt;selectedAnimal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;}),&lt;/span&gt;
      &lt;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;onSelect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;selectedAnimal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;dropdown&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;animalList&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;selected&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;onSelect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;selectedAnimal&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;You&lt;/span&gt; &lt;span class="nx"&gt;selected&lt;/span&gt; &lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="nx"&gt;selectedAnimal&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/template&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we have an animal component, this component displays a dropdown and the selected value of that dropdown. We send the list we want the dropdown to display to that component and we also listen for the event &lt;code&gt;selected&lt;/code&gt;, for which we set the selectedAnimal &lt;code&gt;data&lt;/code&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dropdown&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;list&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;methods&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;onSelect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;$emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;selected&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;change&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;onSelect&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;option&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;item in list&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;item&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;item&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/option&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/template&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dropdown component renders the list given to it by props and emits an event when a value in the dropdown is selected. This shows how data flows down to child components by props and how events can be emitted and listened to by parent components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Computed property
&lt;/h2&gt;

&lt;p&gt;Computed are getters in a component. The result of the getters are cached and will only be recalculated if the values that they depend on in the data property change. Computeds can be used both in the script tag and in the template tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;getMessage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstname&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastname&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So first time this computed is used, the result will be cached and only be reevaluated if the message data attribute changes.&lt;/p&gt;

&lt;p&gt;Computeds are also a good place to put Javascript code that would otherwise be placed in the template and something that the vue template directives don't cover. For example when we only want to iterate in the template over on part of an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;getFilteredArray&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we could just filter over the &lt;code&gt;getFilteredArray&lt;/code&gt; computed in the template instead of involving Javascript directly in the template.&lt;/p&gt;

&lt;h2&gt;
  
  
  Watch property
&lt;/h2&gt;

&lt;p&gt;In the watch property, we can listen for changes in data, computed or props and when they change have a callback that triggers. Like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;watch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doStuff&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When message changes, in this case, we will call doStuff.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lifecycle events
&lt;/h3&gt;

&lt;p&gt;Each instance of a component in the component tree has a life span, in short:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;its created when its inserted into the DOM&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;it gets updated during the time it's in the DOM if the props or data changes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;it gets destroyed when it should be removed from the DOM.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a Vue component, we can listen to these events and when they occur hook on to those events and perform actions. For example, one lifecycle event is called mounted, it's triggered when the component instance is mounted in the DOM. This event will happen one time during the life span of each component instance and when this event happens, we can decide what to do when our component has mounted. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;mounted&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchFromApi&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resultFromApi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most commonly used lifecycle events are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;created, when the component is created and before it's inserted into the DOM&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;mounted, when the component is inserted into the DOM&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;updated, when the component will re-render&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;destroyed, when the component is destroyed&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a full example of when each lifecycle event is triggered, see the &lt;a href="https://vuejs.org/v2/guide/instance.html#Lifecycle-Diagram" rel="noopener noreferrer"&gt;lifecycle diagram on vues docs&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exercises
&lt;/h2&gt;

&lt;p&gt;After reading this article, I hope you have got a good introduction to VueJS. I have created a series of exercises to try out VueJs and building a weather application. Please check out the exercises on my &lt;a href="https://github.com/mStamstrom/vue-weather-app-exercises" rel="noopener noreferrer"&gt;github&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
      <category>exercises</category>
    </item>
    <item>
      <title>Making E2E testing easy with Cypress</title>
      <dc:creator>Marcus Stamström</dc:creator>
      <pubDate>Sun, 28 Apr 2019 11:05:52 +0000</pubDate>
      <link>https://dev.to/mstamstrom/making-e2e-testing-easy-with-cypress-pk5</link>
      <guid>https://dev.to/mstamstrom/making-e2e-testing-easy-with-cypress-pk5</guid>
      <description>&lt;h1&gt;
  
  
  Cypress: Making E2E testing easy
&lt;/h1&gt;

&lt;p&gt;Cypress is an awesome E2E testing framework made to be consistent, fast and easy to use. It is built from scratch so no Selenium. I recently tried it and I found Cypress to be super easy to setup and get started. It was easy to write tests and it really feels fast and solid. Cypress also comes with a great test runner site where you can see your test run live as well as debug tests and much much more.&lt;/p&gt;

&lt;p&gt;What we will go through in this article:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;go through what E2E tests are in the perspective of web apps&lt;/li&gt;
&lt;li&gt;learn about Cypress and its features&lt;/li&gt;
&lt;li&gt;understand how to setup Cypress&lt;/li&gt;
&lt;li&gt;learn how to write E2E tests with Cypress&lt;/li&gt;
&lt;li&gt;go through how to use Cypress test runner&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What and why E2E test?
&lt;/h2&gt;

&lt;p&gt;Testing is all about quality. We want to make sure that our web apps don't contain bugs. Unit testing tests as small parts as possible and testing all the edge cases. E2E testing, on the other hand, tests the whole functionality, from GUI to API and back. In E2E tests, we usually stick to the happy paths, since we covered all the edge cases in our unit tests.&lt;/p&gt;

&lt;p&gt;My definition of E2E testing for web apps:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;E2E testing is to test the web app from a user perspective&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We are testing the application automatically in the browser to make sure that all functionality holds together and stays consistent and correct as the application evolves. I usually see one E2E test as testing one flow in a user story, the E2E test should take the same steps as when testing a user story manually and produce the same end result.&lt;/p&gt;

&lt;p&gt;When E2E testing, it doesn't really matter which framework we use. Consider the web app as a black box that we test through the GUI.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we will test in this article
&lt;/h2&gt;

&lt;p&gt;The test examples in this article will be based on one view from my weather application, which looks like this.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fguu4x7rne9140zox7iud.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fguu4x7rne9140zox7iud.PNG" alt="Weather app page"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Cypress
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The web has evolved. Finally, testing has too.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Cypress is an E2E testing framework. It is built from scratch to reduce inconsistent results and improve speed. It works for both unit, integration and E2E tests. It's free, open-source, has a test runner locally installed and a dashboard service for recording test runs.&lt;/p&gt;

&lt;p&gt;The focus of Cypress is to make it easy to setup Cypress and easy to write, run and debug tests. I really think that Cypress has succeeded with these parts, it's really easy to get going and it's almost as easy to write unit tests with jest as to write E2E tests with Cypress.&lt;/p&gt;

&lt;p&gt;Cypress only supports Chrome of the major browser in the current stage, but they are working on cross-browser support for Firefox, Edge and IE11.&lt;/p&gt;
&lt;h3&gt;
  
  
  Some Cypress features
&lt;/h3&gt;

&lt;p&gt;Cypress has a lot of awesome features. From an E2E perspective, the main features are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stubs and spies&lt;/li&gt;
&lt;li&gt;Time travel to see all the steps taken in each test in the test runner&lt;/li&gt;
&lt;li&gt;Debugging in familiar tools like Chrome dev tools&lt;/li&gt;
&lt;li&gt;Real time reloads&lt;/li&gt;
&lt;li&gt;Control network traffic&lt;/li&gt;
&lt;li&gt;Automatic awaiting&lt;/li&gt;
&lt;li&gt;Screenshots and videos of failing tests&lt;/li&gt;
&lt;li&gt;Consistent results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cypress comes with mocha and chai as the testing framework, so everything is setup and ready to get started.&lt;br&gt;
One of the features mentioned is automatic awaiting. This is really awesome since we now never have to check if an element is in the DOM or perform setTimeout and hope that the element has shown up. Cypress commands automatically waits for the command to be completed and when it's finished it goes to the next command in the test. If the Cypress command isn't finished after 5 seconds, the test will fail.&lt;/p&gt;

&lt;p&gt;For Cypress to use automatic awaiting, all Cypress commands are asynchronous, but they are run synchronously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(...).&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, both get() and click() are asynchronous commands, but get() must be finished before the click command can be run. So each command is like a promise that has to be completed before running the next command.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Cypress tests are more reliable?
&lt;/h3&gt;

&lt;p&gt;One of the features in Cypress is consistent results. Cypress is built from scratch to get more consistent results, but why are E2E tests in Cypress more consistent than for example Selenium based E2E frameworks?&lt;br&gt;
Selenium and similar frameworks operate by running outside of the browser and execute remote commands across the network. Cypress is the opposite and instead operates inside the browser. This is why Cypress can produce more consistent test results and is also much faster in my opinion. This also enables Cypress to provide other nice features like mocking and capturing network traffic.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up Cypress
&lt;/h2&gt;

&lt;p&gt;Cypress has done it very easy for us to get up and running. First, install Cypress:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; cypress
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will download everything Cypress needs. After that just open Cypress:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  ./node_modules/.bin/cypress open
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First time this command is run, Cypress will setup everything for you to start writing E2E tests. Cypress also includes an example folder with many valuable examples and tips.&lt;/p&gt;

&lt;p&gt;But this command isn't really about the setup. What this command really does is opening the test runner. In the test runner, we can see our E2E test runs, debug our tests and other things. The Cypress start commands should be added to the package.json.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "cypress:open": "cypress open",
  "cypress:run": "cypress run"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Cypress run command will run the tests in the terminal, which is useful on the CI for example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing our first test
&lt;/h2&gt;

&lt;p&gt;After setting up Cypress we can start writing E2E tests. So let's look at how an E2E test can look. In this test, we want to check that the main temperature changes when selecting one of the items in the forecast list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foq9gmvxqpuh5kzvj5j8m.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foq9gmvxqpuh5kzvj5j8m.PNG" alt="code example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cypress commands don't return the element and instead uses chaining. But since Cypress commands are promise like and promises are thenable, we can wait for the Cypress command to finish and the get the element produced by the Cypress command with &lt;code&gt;then()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note that cypress commands are Promise like, but not exactly a Promise. So we can use then for triggering a callback when the command is finished, but we cannot use async/await.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stubs (mocks)
&lt;/h2&gt;

&lt;p&gt;Mocking is a great way to get control over certain aspects of the app. Even if we are testing a black box, it's very useful to for instance get control over the API or the geolocation, so we can focus some E2E tests on other aspects.&lt;/p&gt;

&lt;p&gt;In Cypress, we mock by using stubs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;replacerFunction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, for example, we can stub &lt;code&gt;fetch&lt;/code&gt; for certain tests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqncwu26ennxkw26hepj.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqncwu26ennxkw26hepj.PNG" alt="code example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This way we can make sure that our app presents exactly the correct information given a certain response from the API.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Stubs should be located in the onBeforeLoad, which makes the stubs available in the current visit of localhost.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qyj2ewtlzyziu5ozwym.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qyj2ewtlzyziu5ozwym.PNG" alt="code example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is another stub, where we want the geolocation to be the center of Stockholm, Sweden. This way when running this test, we will always fetch the weather for Stockholm no matter where this test is run.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to mock and when to not?
&lt;/h2&gt;

&lt;p&gt;It's really nice to be able to mock certain things and knowing what to expect in return. Though we shouldn't always mock, it should be a good mix. First, write a few E2E tests without stubs if possible. So we first test all the way from end to end and make sure that all integrations work. Then we can start using stubs to make sure our GUI presents exactly the correct information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing tests with mocks
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhap44zgpv0xl7qafhk8c.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhap44zgpv0xl7qafhk8c.PNG" alt="code example"&gt;&lt;/a&gt;&lt;br&gt;
Here is the full test of when we mock fetch to test that our GUI presents correct information. In this test, we use the contain command, which checks if the fetched element contains the input value. This command works both for a specific element and for a container of elements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test runner
&lt;/h2&gt;

&lt;p&gt;Cypress comes with an awesome test runner. This is a really great tool and has many features. It's in the test runner that we see our test runs, debug our tests and see that our tests take the correct path through the application. The test runner runs in a chrome instance and thereby giving access to chrome devtools for debugging.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5lk3i024t5c2zxqlqh16.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5lk3i024t5c2zxqlqh16.PNG" alt="Test runner"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So here is a print of one set of E2E tests. We can run all the tests from here and also open chrome devtools to inspect elements or debug our web app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjo64crh51lunpzgdgi3r.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjo64crh51lunpzgdgi3r.gif" alt="Test runner in action"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a GIF of me inspecting an E2E test. When hovering on different steps in the E2E test, the test runner visualizes which element the Cypress command affects. It also shows which state the GUI was in at each step in a test. This is what Cypress call time travel, we can travel through each step of an E2E test and see how the GUI looked at each step. This is awesome for debugging and just to see that our GUI behaves as expected. Notice also that when hovering on the click event, the test runner shows the before and after of that click.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That is all for this article. Hope you enjoyed it and that I inspired you to try out Cypress for E2E testing.&lt;br&gt;
In this article, we first looked at what E2E testing is. Then we talked about some of the features in Cypress and went deeper into setup, stubs, how to write E2E tests and the test runner with time travel in the end.&lt;/p&gt;

&lt;p&gt;If you want to read more about Cypress, they have great docs at Cypress.io with examples and guides for how to write E2E tests.&lt;/p&gt;

&lt;p&gt;If you want to see the whole weather app it is live at GitHub pages. If you are interested in seeing more of my E2E tests, the project is available on my GitHub.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.cypress.io" rel="noopener noreferrer"&gt;Cypress docs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/mStamstrom/weather-app" rel="noopener noreferrer"&gt;Github project&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cypress</category>
      <category>javascript</category>
      <category>e2etesting</category>
    </item>
  </channel>
</rss>
