<?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: Mikhail Kuznetcov</title>
    <description>The latest articles on DEV Community by Mikhail Kuznetcov (@legkoletat).</description>
    <link>https://dev.to/legkoletat</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%2F88544%2F24c4d813-7d79-495a-82e8-5dae0cdf619c.jpeg</url>
      <title>DEV Community: Mikhail Kuznetcov</title>
      <link>https://dev.to/legkoletat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/legkoletat"/>
    <language>en</language>
    <item>
      <title>Feedback form with Svelte</title>
      <dc:creator>Mikhail Kuznetcov</dc:creator>
      <pubDate>Sat, 19 Oct 2019 18:56:29 +0000</pubDate>
      <link>https://dev.to/legkoletat/feedback-form-with-svelte-e2f</link>
      <guid>https://dev.to/legkoletat/feedback-form-with-svelte-e2f</guid>
      <description>&lt;p&gt;In this article I will show you how you can setup a Svelte project, add basic frontend form functionality with Svelte and then build the project.&lt;/p&gt;

&lt;p&gt;If we are considering to building practical piece of UI - I'd propose choose a feedback form for example. Internet already has millions of todo-app tutorials. Our form will have multiple fields, basic validation and option to send data to a backend API using fetch API.&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%2Fbeaod06dpahklyjdbkzn.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%2Fbeaod06dpahklyjdbkzn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Svelte&lt;/strong&gt;&lt;br&gt;
About 3 years ago Svelte library was created by &lt;a href="https://twitter.com/Rich_Harris" rel="noopener noreferrer"&gt;Rich Harris&lt;/a&gt;. One of the biggest differences of that new framework was that it’s not relying on Virtual DOM like popular modern solutions, such as ReactJS or VueJS.&lt;br&gt;
According to its author Svelte however is more than just a framework, it's an another approach towards easier building reactive UIs, language if you will - &lt;a href="https://gist.github.com/Rich-Harris/0f910048478c2a6505d1c32185b61934" rel="noopener noreferrer"&gt;read more on this in this gist&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Bootstrap Svelte project&lt;/h2&gt;

&lt;p&gt;We can create a new project using following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npx degit sveltejs/template &amp;lt;FOLDER_NAME_HERE&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;What we do here is call a &lt;code&gt;degit&lt;/code&gt; tool to generate the new project using a template config named &lt;code&gt;sveltejs/template&lt;/code&gt; in the folder that you provide as a last parameter.&lt;br&gt;
Since we use &lt;code&gt;npx&lt;/code&gt; &lt;a href="https://www.npmjs.com/package/npx" rel="noopener noreferrer"&gt;npx documentation here&lt;/a&gt; we are not installing &lt;code&gt;degit&lt;/code&gt; into the system, but you can always install it like this&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm install -g degit

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;then scaffolding next Svelte app (in this case it's just cloning the template repo) will be faster, just by calling &lt;code&gt;degit sveltejs/template &lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Ok, so the script was fast to run, let got to the folder and see the structure we have there:&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%2Fstvxup5rjfur3kc74jzj.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%2Fstvxup5rjfur3kc74jzj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
I will be using Visual studio code for further development. We can see we have a single svelte file created, that contains basic code, we have a config for a bundler (Rollup is default, however, you can also use Parsel and Webpack) that is used &lt;code&gt;rollup.config.js&lt;/code&gt; and a &lt;code&gt;package.json&lt;/code&gt;. Let's check what commands we have in it:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
  "scripts": {
    "build": "rollup -c",
    "autobuild": "rollup -c -w",
    "dev": "run-p start:dev autobuild",
    "start": "sirv public --single",
    "start:dev": "sirv public --single --dev"
 }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So to start developing we will need to run &lt;code&gt;dev&lt;/code&gt; command. What it will do - it'll run in parallel (thanks to special &lt;code&gt;run-p&lt;/code&gt; command) the static assets server - &lt;code&gt;sirv public&lt;/code&gt; and a bundler in a watch mode: &lt;code&gt;rollup -c -w&lt;/code&gt;. This will provide us with live update of the page any code files changes&lt;/p&gt;

&lt;p&gt;So as we start we should see such output in the console:&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%2Fepci0t4gne2auypgidx6.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%2Fepci0t4gne2auypgidx6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
It mean all went well so let's open &lt;code&gt;localhost:5000&lt;/code&gt; in the browser and see what we'll get. You'll see a really basic output which only contains one line of text with dynamic variable &lt;code&gt;{name}&lt;/code&gt; in it.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Concerning the other files and app structure the sequence is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Other components -&amp;gt; App.svelte -&amp;gt; main.js 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So app instance initialisation code is in the &lt;code&gt;main.js&lt;/code&gt; file and since they're pretty simple it looks like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;During the compilation step it is all consumed by Rollup or other bundler and it wild output it in index.html and &lt;code&gt;/public&lt;/code&gt; folder.&lt;/p&gt;

&lt;h2&gt;Form logic&lt;/h2&gt;

&lt;p&gt;Currently our app.svelte file doesn't contain much. So let's get into it and add some UI markup. You can write with yourself or you can copy it from some &lt;a href="https://bootsnipp.com/similar/vO0d" rel="noopener noreferrer"&gt;Bootstrap form samples page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So here's how your form may look like. No dynamic elements, just HTML markup.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Markup in Svelte is pretty easy, all the CSS, JS and HTML can be combined in one file (same you can do in &lt;a href="https://vuejs.org/v2/guide/single-file-components.html" rel="noopener noreferrer"&gt;Vue&lt;/a&gt;). All the JS code is between the script-tags, and the component styles are... you guessed it, is in style tags. Good to know that CSS written in svelte files is scoped.&lt;/p&gt;

&lt;p&gt;Again, if using Visual Studio code for editing I would recommend to install &lt;a href="https://github.com/UnwrittenFun/svelte-vscode" rel="noopener noreferrer"&gt;extension svelte-vscode&lt;/a&gt; that will manage code highlighting and many more for you.&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%2Filu9kzb9o7nphvw9w2ny.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%2Filu9kzb9o7nphvw9w2ny.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;State&lt;/strong&gt;&lt;br&gt;
What is the most important to do right in modern frontend apps - of course it's the State. It's easy to start building something small ignoring the right approach to managing the state and then find yourself in situation that you need to refactor all the code you recently wrote.  Everything depends on everything, unexpected side effects start to appear.&lt;br&gt;
So let's create a simple state object:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
It is quite simple but later you may want to put it into Redux or persist in localStorage or what will you.

&lt;p&gt;&lt;strong&gt;Form&lt;/strong&gt;&lt;br&gt;
Let's add a markup with using several Svelte features:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let's see what are those:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;{#if STATE.send.success}&lt;/code&gt; - way to show/hide certain blocks of markup based on the model value; &lt;code&gt;{#}...{/}&lt;/code&gt; is a common way to structure templates in Svelte;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{@debug STATE} &lt;/code&gt; - offers an alternative to console.log(...). It logs the values of specific variables whenever they change, and pauses code execution if you have devtools open;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;on:click={validateAndSend}&lt;/code&gt; - Svelte way to bind event handlers on DOM element; in this case it's a button;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Sending data&lt;/h2&gt;

&lt;p&gt;In the JS logic the &lt;code&gt;validateAndSend&lt;/code&gt; function will do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;retrieving of data using FormData &lt;/li&gt;
&lt;li&gt;validation &lt;/li&gt;
&lt;li&gt;if all is successful it will send it to backend&lt;/li&gt;
&lt;li&gt;update the UI with success/failure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's how its looks like:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;All the functionality that can be separated such as validation and sending data over the network I've extracted to separate &lt;code&gt;/service.js&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building the app&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now if you see that the functionality that we expected is working as it should let's try to run the production build with: &lt;code&gt;npm run build&lt;/code&gt;&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%2Fvndltsxknr0sct151sqi.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%2Fvndltsxknr0sct151sqi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Look at the time it took to build the whole app. Of course our app is really tiny but still your can see that the overhead for the framework part of the build is negligible in case of Svelte.&lt;/p&gt;

&lt;p&gt;Here's how the generated html-page looks like,&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
It contains one link to the JS bundle and separate links to stylesheets that you may want to use. It comes really handy when you have part of your styles refused from readymade style sheet - like using Bootstrap or material design CSS.

&lt;p&gt;Here's a basic feedback form app that you can build with Svelte. &lt;br&gt;
Hope some of the framework features while reading this article, along with the approaches towards managing modules and state in frontend apps.&lt;/p&gt;

&lt;p&gt;All the code is also stored in the separate &lt;a href="https://github.com/shershen08/feedbackform-svelte" rel="noopener noreferrer"&gt;shershen08/feedbackform-svelte repo &lt;/a&gt;. Please have look at it if you'll have any questions.&lt;/p&gt;

&lt;p&gt;I'll be happy to answer your questions in the comments or in Twitter: &lt;a href="https://twitter.com/legkoletat" rel="noopener noreferrer"&gt;@legkoletat&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>svelte</category>
      <category>sveltejs</category>
      <category>howto</category>
      <category>modernjavascript</category>
    </item>
  </channel>
</rss>
