<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Taras Batenkov on Medium]]></title>
        <description><![CDATA[Stories by Taras Batenkov on Medium]]></description>
        <link>https://medium.com/@enkot?source=rss-992e943bc0b0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Z_n80h55r7adoVnzhDtbEg.jpeg</url>
            <title>Stories by Taras Batenkov on Medium</title>
            <link>https://medium.com/@enkot?source=rss-992e943bc0b0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 17:29:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@enkot/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Vue.js 3: Future-Oriented Programming]]></title>
            <link>https://medium.com/bitsrc/vue-js-3-future-oriented-programming-54dee797988b?source=rss-992e943bc0b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/54dee797988b</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Taras Batenkov]]></dc:creator>
            <pubDate>Thu, 01 Aug 2019 15:01:19 GMT</pubDate>
            <atom:updated>2023-05-23T04:24:38.623Z</atom:updated>
            <content:encoded><![CDATA[<h4>How function-based API solves logic reusability problem</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WSN4Dx07bs92MlVV3tTaSw.png" /></figure><p>If you are interested in Vue.js, you probably know about the 3rd version of this framework, which will be released shortly (if you are reading this article from the future, I hope it’s still relevant 😉). The new version is under active development for now, but all possible features can be found in separate RFC (request for comments) repository: <a href="https://github.com/vuejs/rfcs">https://github.com/vuejs/rfcs</a>. One of them, <a href="https://github.com/vuejs/rfcs/blob/function-apis/active-rfcs/0000-function-api.md"><strong>function-api</strong></a>, can dramatically change the style of developing Vue apps.</p><blockquote><em>This article is aimed at people who have at least some background in JavaScript and Vue.</em></blockquote><p>Before we start: Use <a href="https://bit.dev/?utm_medium=content&amp;utm_source=bitsandpieces&amp;utm_content=5&amp;utm_campaign=campaign"><strong>Bit</strong></a> to <strong>encapsulate Vue components</strong> with all their dependencies and setup. Build truly modular applications with better code reuse, simpler maintenance and less overhead. <strong>Share and collaborate on individual Vue components.</strong></p><h4>Learn more:</h4><p><a href="https://bit.dev/blog/unlock-the-power-of-vuejs-with-bit-lgah52ts/">Unlock the Power of Vue.js with Bit</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*k5QhKBVlbZrid4LDkeCK7g.gif" /><figcaption>Components with Bit: Easily share across projects as a team</figcaption></figure><h3>What’s wrong with the current API? 👀</h3><p>The best way is to show everything in an example. So, let’s imagine that we need to implement a component that should fetch some user’s data, show loading state and topbar depending on scroll offset. Here is the final result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*QQigXylzQ95jxCb_FI_dCQ.gif" /></figure><p>Live example you can check <a href="https://enkot.github.io/vue-function-api-test/"><strong>here</strong></a>.</p><p>It is good practice to extract some logic to reuse across multiple components. With Vue 2.x’s current API, there are a number of common patterns, most well known are:</p><ul><li>Mixins (via the mixins option) 🍹</li><li>Higher-order components (HOCs) 🎢</li></ul><p>So, let’s move scroll tracking logic into a mixin, and fetching logic into a higher-order component. Typical implementation with Vue you can see below.</p><p>Scroll mixin:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6d3d867e0aa0bb9717c178ce300dbaa/href">https://medium.com/media/c6d3d867e0aa0bb9717c178ce300dbaa/href</a></iframe><p>Here we add scroll event listener, track page offset and save it in pageOffset property.</p><p>The higher-order component will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67f6bdb8a793e9725a2ddb9da30f46da/href">https://medium.com/media/67f6bdb8a793e9725a2ddb9da30f46da/href</a></iframe><p>Here isLoading, posts properties initialized for loading state and posts data respectively. The fetchPosts method will be invoked after creating an instance and every time props.id changes, in order to fetch data for new id.</p><p>It’s not a complete implementation of HOC, but for this example, it will be enough. Here we just wrap the target component and pass original props alongside fetch-related props.</p><p>Target component looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12378511072d8e451bc8a58c4e8d6f9b/href">https://medium.com/media/12378511072d8e451bc8a58c4e8d6f9b/href</a></iframe><p>To get specified props it should be wrapped in created HOC:</p><pre><em>const</em> PostsPage = <strong>withPostsHOC(</strong>PostsPage<strong>)</strong></pre><blockquote>Full component with template and styles can be found <a href="https://github.com/enkot/vue-function-api-test/blob/master/src/components/PostsPageOptions.vue"><strong>here</strong></a>.</blockquote><p>Great! 🥳 We just implemented our task using mixin and HOC, so they can be used by other components. But not everything is so rosy, there are several problems with these approaches.</p><h4>1. Namespace clashing ⚔️</h4><p>Imagine that we need to add update method to our component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49fe3efb1418b50577b7468c837d21c2/href">https://medium.com/media/49fe3efb1418b50577b7468c837d21c2/href</a></iframe><p>If you open the page again and scroll it, the topbar will not be shown anymore. This is due to the overwriting of mixin’s method update. The same works for HOCs. If you change the data field fetchedPosts to posts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4feae68e4fe1bbe8dd5071939681a0a/href">https://medium.com/media/a4feae68e4fe1bbe8dd5071939681a0a/href</a></iframe><p>…you will get errors like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9xefHCvM1wre_X4PxBFnvQ.png" /></figure><p>The reason for this is that wrapped component already specified property with the name posts.</p><h4>2. Unclear sources 📦</h4><p>What if after some time you decided to use another mixin in your component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3895921c3fe66774d196885986090426/href">https://medium.com/media/3895921c3fe66774d196885986090426/href</a></iframe><p>Can you tell exactly which mixin a pageOffset property was injected from? Or in another scenario, both mixins can have, for example, yOffset property, so the last mixin will override property from the previous one. That’s not good and can cause a lot of unexpected bugs. 😕</p><h4>3. Performance ⏱</h4><p>Another problem with HOCs is that we need separate component instances created just for logic reuse purposes that come at a performance cost.</p><h3>Let’s “setup” 🏗</h3><p>Let’s see what alternative can offer the next Vue.js release and how we can solve the same problem using function-based API.</p><p>Since Vue 3 is not released yet, the helper plugin was created — <a href="https://github.com/vuejs/vue-function-api"><strong>vue-function-api</strong></a>. It provides function api from Vue3.x to Vue2.x for developing next-generation Vue applications.</p><p>Firstly, you need to install it:</p><pre>$ <strong>npm</strong> install vue-function-api</pre><p>and explicitly install via Vue.use():</p><pre>import Vue from &#39;vue&#39;<br>import { plugin } from &#39;vue-function-api&#39;<br><br>Vue.use(plugin)</pre><p>The main addition function-based API provides is a new component option - setup(). As the name suggests, this is the place where we use the new API’s functions to setup the logic of our component. So, let’s implement a feature to show topbar depending on scroll offset. Basic component example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3999a45aad9b3d51db1208183acddb5/href">https://medium.com/media/c3999a45aad9b3d51db1208183acddb5/href</a></iframe><p>Note that the setup function receives the resolved props object as its first argument and this props object is reactive. We also return an object containing pageOffset property to be exposed to the template’s render context. This property becomes reactive too, but on the render context only. We can use it in the template as usual:</p><pre>&lt;div <em>class</em>=&quot;topbar&quot; <em>:class</em>=&quot;{ open: <strong>pageOffset</strong> &gt; 120 }&quot;&gt;...&lt;/div&gt;</pre><p>But this property should be mutated on every scroll event. To implement this, we need to add scroll event listener when the component will be mounted and remove the listener — when unmounted. For these purposes value, onMounted, onUnmounted API functions exist:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89fcbc1beed371e8c2b8b20c900b54a4/href">https://medium.com/media/89fcbc1beed371e8c2b8b20c900b54a4/href</a></iframe><p>Note that all lifecycle hooks in 2.x version of Vue have an equivalent onXXX function that can be used inside setup().</p><p>You probably also noticed that pageOffset variable contains a single reactive property: .value. We need to use this wrapped property because primitive values in JavaScript like numbers and strings are not passed by reference. Value wrappers provide a way to pass around mutable and reactive references for arbitrary value types.</p><p>Here’s how the pageOffset object looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gIwlwDmJ0S-i_bDpSf6m4A.png" /></figure><p>The next step is to implement the user’s data fetching. As well as when using option-based API, you can declare computed values and watchers using function-based API:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cf043367630708e6f7f891fac04445c/href">https://medium.com/media/4cf043367630708e6f7f891fac04445c/href</a></iframe><p>A computed value behaves just like a 2.x computed property: it tracks its dependencies and only re-evaluates when dependencies have changed. The first argument passed to watch is called a &quot;source&quot;, which can be one of the following:</p><ul><li>a getter function</li><li>a value wrapper</li><li>an array containing the two above types</li></ul><p>The second argument is a callback that will only get called when the value returned from the getter or the value wrapper has changed.</p><p>We just implemented the target component using function-based API. 🎉 The next step is to make all this logic reusable.</p><h3>Decomposition 🎻 ✂️</h3><p>This is the most interesting part, to reuse code related to a piece of logic we just can extract it into what called a “composition function” and return reactive state:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/866576e9ffcfe9ea8194e4e14425040c/href">https://medium.com/media/866576e9ffcfe9ea8194e4e14425040c/href</a></iframe><p>Note how we used useFetchPosts and useScroll functions to return reactive properties. These functions can be stored in separate files and used in any other component. Compared to the option-based solution:</p><ul><li>Properties exposed to the template have clear sources since they are values returned from composition functions;</li><li>Returned values from composition functions arbitrarily named so there is no namespace collision;</li><li>There are no unnecessary component instances created just for logic reuse purposes.</li></ul><p>There are a lot of other benefits that can be found on the <a href="https://github.com/vuejs/rfcs/blob/function-apis/active-rfcs/0000-function-api.md"><strong>official RFC page</strong></a><strong>.</strong></p><p>All code examples used in this article you can find <a href="https://github.com/enkot/vue-function-api-test"><strong>here</strong></a>.</p><p>Live example of the component you can check <a href="https://enkot.github.io/vue-function-api-test/"><strong>here</strong></a>.</p><h3>Conclusion</h3><p>As you can see Vue’s function-based API presents a clean and flexible way to compose logic inside and between components without any of option-based API drawbacks. Just imagine how powerful composition functions could be for any type of project — from small to big, complex web apps. 🚀</p><p>I hope this post was useful 🎓. If you have any thoughts or questions, please feel free to respond and comment below! I will be glad to answer 🙂. Thanks.</p><h3>Related Stories</h3><ul><li><a href="https://blog.bitsrc.io/sharing-and-reusing-vue-mixins-in-the-cloud-with-bit-dev-830104a48d0b">Sharing and Reusing Vue Mixins in the cloud with Bit.dev</a></li><li><a href="https://blog.bitsrc.io/5-tools-for-faster-vue-js-app-development-ad7eda1ee6a8">5 Tools for Faster Vue.js App Development</a></li></ul><h3>Encapsulates components with Bit to run them anywhere across your projects and applications</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FE5lgoz6-nfs%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DE5lgoz6-nfs&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FE5lgoz6-nfs%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/584325f6121c810af8af8824913e9402/href">https://medium.com/media/584325f6121c810af8af8824913e9402/href</a></iframe><p><a href="https://bit.dev/?utm_medium=content&amp;utm_source=bitsandpieces&amp;utm_content=6&amp;utm_campaign=bottom"><strong>Bit</strong></a><strong> </strong>encapsulates components in your projects with all their files and dependencies, so they can run anywhere across your applications.</p><p>Build faster by making your components reusable out-of-the-box, and collaborate as a team to share and discover components. No refactoring or configurations needed, just share components and build truly modular apps.</p><h4><a href="https://bit.dev/?utm_medium=content&amp;utm_source=bitsandpieces&amp;utm_content=6&amp;utm_campaign=bottom">LEARN MORE</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54dee797988b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bitsrc/vue-js-3-future-oriented-programming-54dee797988b">Vue.js 3: Future-Oriented Programming</a> was originally published in <a href="https://medium.com/bitsrc">Bits and Pieces</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Better errors handling for ES/Typescript classes]]></title>
            <link>https://medium.com/bitsrc/errors-handling-for-vue-class-components-2f152f7c7515?source=rss-992e943bc0b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f152f7c7515</guid>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Taras Batenkov]]></dc:creator>
            <pubDate>Mon, 27 Aug 2018 15:00:15 GMT</pubDate>
            <atom:updated>2019-08-20T19:26:56.309Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Jw7OaJ_6Jr-F9jOfbIfrQ.png" /></figure><h4>Writing a custom catch decorator.</h4><p>In general, errors handling in JavaScript is not a very complicated topic, but it is very useful. When different errors occur during your application’s life, break it and leave the user bewildered by what is happening. 😵</p><p>If you want to protect a part of your code from possible errors, you can just run and test a block of code for errors and, if they occur, handle them and notify user with some message.</p><p>All of this can be done using try/catch blocks or .catch method of Promise objects. So far so good. In this post, we’ll dive in to bringing theory to practice with error handling in JavaScript applications. Let’s get started.</p><blockquote><em>This post assumes basic-decent background in JavaScript.</em></blockquote><p>All next examples will be shown in case of <a href="https://vuejs.org/">Vue</a> class components, as typical errors handling situations. But it can be used with any ES/Typescript classes!</p><p>Moving on. ⏬</p><p><strong>Tip:</strong> Use <a href="https://bit.dev/"><strong>Bit</strong></a> to <strong>encapsulate modules/components</strong> with all their dependencies and setup. Share them in <a href="https://bit.dev/">Bit’s cloud</a> and collaborate with your team.</p><figure><a href="https://bit.dev/"><img alt="" src="https://cdn-images-1.medium.com/max/800/0*YQOZvYD4aiYv9jFg.gif" /></a></figure><p>You should “try” 🧗‍</p><p>Most tutorials regarding error handling in Vue components (usually errors are displayed as “toast” messages to the user) will show an implementation that looks something like this (login example):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49b85ffff3db6b974811cff7e09824b2/href">https://medium.com/media/49b85ffff3db6b974811cff7e09824b2/href</a></iframe><p>The above implementation is correct and looks great.</p><ul><li>We make an API request and wait for a response (token)</li><li>If there are no errors → handle response</li><li>If an error is catched in loginUser method → go to catch block and handle error</li></ul><p>This looks easy 😎.</p><p>But, what happens when your application is larger that a small tutorial and it contains a lot of API requests and other stuff which can throw errors?</p><p>Well, it will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a031d9615adef58cef00bc92adb68e2d/href">https://medium.com/media/a031d9615adef58cef00bc92adb68e2d/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ccd0985b851f545e0bea8e1811901b1/href">https://medium.com/media/9ccd0985b851f545e0bea8e1811901b1/href</a></iframe><p>As you can see, alot of try/catch blocks make for some pretty code ugly, imperative and repetitive code (the same errors handling using toast notifications).</p><p>It’s better to make your code more declarative, describing <strong><em>what</em></strong> the program must accomplish, rather than just describe <strong><em>how</em></strong> to accomplish it.</p><p>So, we should find a way to hide this try/catch logic and find a more elegant solution to handle errors in our Vue.js app. Let’s give it a go.</p><h3>Vue instruments ⛏</h3><p>If you open Vue’s documentation, you can find a global options property named errorHandler, to which a handler for uncaught errors can be assigned. This should be the perfect option for us, so let’s try:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f47130e1d5b9a4dda9e38a0b7549852d/href">https://medium.com/media/f47130e1d5b9a4dda9e38a0b7549852d/href</a></iframe><p>Great! 😃 We removed the ugly try/catch blocks and moved handling to theglobal errorHandler method. Let’s start the app:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wHKrnF6dn7jeauaZKcQFDg.png" /><figcaption>Chrome DevTools console</figcaption></figure><p>Hmmm… Error from mounted hook catched, but the one from created is not. The reason for this (Vue’s <a href="https://vuejs.org/v2/api/#errorHandler">documentation</a> doesn’t tell anything about it) is that the errorHandler doesn’t catch errors in <strong>asynchronous</strong> methods (and native event handlers). Internally, Vue runs our created hook as <strong>synchronous</strong>, not waiting for<strong> </strong>completion of an asynchronous operation, when an error can actually be thrown.</p><p>Moving on. ⏬</p><h3>Let’s catch 🎣</h3><p>During research, I stumbled upon some Java code and saw interesting error handling, natively supported in Java:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afef619306157505b523c0b44f97bd03/href">https://medium.com/media/afef619306157505b523c0b44f97bd03/href</a></iframe><p>Here throws IOException tells the compiler that some exception can be thrown in this method and should be handled. It would be great to implement something similar in our case.</p><p>However, JavaScript doesn’t have a native mechanism which allows us to implement such a functionality. True… but it does have decorators! 😈</p><p>A <a href="https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841">Decorator</a> is a programming pattern, and below you can see one of it’s implementations:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5643e4da37ee1bc42b938d7b9d5d45b3/href">https://medium.com/media/5643e4da37ee1bc42b938d7b9d5d45b3/href</a></iframe><p>For those who hear about it for the first time (<a href="https://refactoring.guru/design-patterns/decorator">read more</a>), function log in the example above is a decorator. It takes getData function as an argument and extends the behavior of the latter function with logging functionality.</p><blockquote><strong>Decorator</strong> is a structural design pattern that lets you attach new behaviors to objects by placing them inside wrapper objects that contain these behaviors.</blockquote><p>But JavaScript has it’s own decorators implementation — a function that is invoked with a prefixed @ symbol, and immediately followed by a class, parameter, method or property:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d6b96240d859a178f0fffeb48830995/href">https://medium.com/media/0d6b96240d859a178f0fffeb48830995/href</a></iframe><p>Here, @Catch() annotation is our decorator. Let’ implement it. Referencing the ES2016 specification, a decorator is an expression which returns function and can take as arguments:</p><ul><li>target — class, where this decorator is actually used;</li><li>name — method/property name to which this decorator is applied;</li><li>property descriptor—specific object that describes the property’s attributes.</li></ul><p>Base decorator:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f3f7da4d87c2328524dcc57bedc5b3b/href">https://medium.com/media/1f3f7da4d87c2328524dcc57bedc5b3b/href</a></iframe><p>First of all, we save the descriptor value to originalMethod. Then rewrite the original value with our version of target function, where we run original method with arguments from wrapper function.</p><p>We use .apply to link the method with an original context(this). Calling original method wrapped with try/catch block, and, for now, just show messages on any error. After that, return descriptor back.</p><blockquote><strong>Note: </strong>await method works for both sync and async functions.</blockquote><p>Using our custom decorator in a component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6be95ae9d62a4708cc345a37a13fbbd0/href">https://medium.com/media/6be95ae9d62a4708cc345a37a13fbbd0/href</a></iframe><p>Let’s start the app:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wcy9nUQS5HK0eSkG0ej0GA.png" /></figure><p>It works! 🎉 Now synchronous (from mounted hook) and asynchronous (from created hook) errors are properly handled and all this works with only one annotation.</p><p>You can also use it for regular Promise without async/await, it will work too:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc7815c3a48f5b5f0121a250af07dc5b/href">https://medium.com/media/dc7815c3a48f5b5f0121a250af07dc5b/href</a></iframe><p>For now, our decorator has a hardcoded error handler, and this solution is not very flexible. So, let’s create some store where we can register custom handler, in order to give the decorator the possibility to get this handler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7ba4be2ff36511f239b236bec22b1a2/href">https://medium.com/media/f7ba4be2ff36511f239b236bec22b1a2/href</a></iframe><p>Here we created a catchDecoratorStore object with asetHandler method which set the handler to handler property. Then in catch block we check if handler exists, if so — run it, otherwise — show default console message.</p><p>Now we can use it very similar to how we used Vue global error handler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a43bc42544a8d2e97db4be58c0b7f68/href">https://medium.com/media/4a43bc42544a8d2e97db4be58c0b7f68/href</a></iframe><p>Almost finished. It’s now a fully working implementation and you can use a decorator for any method, event handler and lifecycle hook.</p><p>But, what if some method needs a separate error handling logic?. In this case we should add an additional functionality to our decorator — the ability to accept arguments, in order to use it like this: @Catch(handler).</p><p>The updated version:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17c70a0db8e28c04d8a5d81ec01858a4/href">https://medium.com/media/17c70a0db8e28c04d8a5d81ec01858a4/href</a></iframe><p>Here the Catch function takes a handler as an argument and returns another function which is actually a decorator. The next change made here (<em>line 11</em>) is checking if the handler is passed to the decorator, and if so, we call it with an error object and the context (current object, where target method is located).</p><p>Our examples context is a Vue component. If the handler didn’t pass, we’ll call a global registered handler and console a message in other case.</p><p>With these last changes you can write components like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d53850e9ca76859bd5c1daf8bf7087ed/href">https://medium.com/media/d53850e9ca76859bd5c1daf8bf7087ed/href</a></iframe><p>That’s it, you can now use global handler or, for special cases, local handlers. It also works with any ES6/Typescript classes.</p><p>You can find all the code from this article <a href="https://github.com/enkot/article-CatchDecorator">here</a>, clone the repository and play with the source code.</p><p>🧐 I also wrote a catch-decorator library with the same concepts as in this article, but with some extended functionality, which you can find on <a href="https://github.com/enkot/catch-decorator">Github</a> and install from NPM:</p><pre>npm install <strong>catch-decorator</strong></pre><h3>Conclusion</h3><p>Errors handling can be done in many ways, and you should find the best solution for your tasks and context. When a project is not very big, then moving errors handling to a separate makeRequest function for API response errors will be enough. But, when you use class components and want to create a unified way to handle errors in your app, decorators can be helpful.</p><p>I hope this post was useful 🎓. If you have any thoughts or questions, please feel free to respond and comment below! I will be glad to answer 🙂. Thanks.</p><h3>Shared with ❤️ in Bit’s blog</h3><p>Components are building blocks. You are the architect.</p><p>Imagine all your components organized on the cloud, made discoverable for your team and synced in all your projects. Give it a try, it’s free &amp; open source.</p><p><a href="https://bit.dev/">Bit. Composable software.</a></p><h3>Learn more</h3><ul><li><a href="https://blog.bitsrc.io/how-to-build-vue-components-like-a-pro-fd89fd4d524d">How to Build Vue Components like a Pro 😎</a></li><li><a href="https://blog.bitsrc.io/how-to-write-better-code-in-react-best-practices-b8ca87d462b0">How To Write Better Code in React</a></li><li><a href="https://blog.bitsrc.io/5-tools-for-faster-vue-js-app-development-ad7eda1ee6a8">5 Tools for Faster Vue.js App Development</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f152f7c7515" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bitsrc/errors-handling-for-vue-class-components-2f152f7c7515">Better errors handling for ES/Typescript classes</a> was originally published in <a href="https://medium.com/bitsrc">Bits and Pieces</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Step by Step! React, Redux and Webpack]]></title>
            <link>https://codeburst.io/its-easy-setting-up-react-and-webpack-eb9ecaef5094?source=rss-992e943bc0b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb9ecaef5094</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Taras Batenkov]]></dc:creator>
            <pubDate>Wed, 26 Jul 2017 08:41:20 GMT</pubDate>
            <atom:updated>2018-03-30T08:55:16.425Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gjvOm1784_sTjT2GOkFTtw.jpeg" /></figure><h4>Part 1. Setting up React environment</h4><p>The purpose of writing this article was to help beginners to start easily with React and answer their main question — How the hell does it work??? 👿</p><p>In this article, I want to describe workflow as clearly as I can, with all the explanations such as “why we need this library” and code examples step by step. I remember my first time with React and Webpack so I know with what problems beginners may encounter. This article consist of two main parts:</p><ol><li>Setting up React environment</li><li>Creating project management app</li></ol><p>If you can’t wait, you can go to the final Github repository: <a href="https://github.com/BatenkovT/Minimal-React">https://github.com/enkot/Minimal-React</a>. 🏃</p><blockquote>This article is aimed at people who want to start with React, have at least some background in JavaScript and familiar with Node.</blockquote><h3><strong>Getting started</strong></h3><p>As mentioned above you need <a href="https://nodejs.org/en/download/package-manager/"><em>Node</em></a> installed on your machine to continue working<strong>. </strong>In this project <em>Yarn Package Manager</em> used (why? see the link below), but you can still use NPM (comes with Node). How to install Yarn you can find <a href="https://yarnpkg.com/lang/en/docs/install/"><strong>here</strong></a><strong>.</strong></p><p><a href="https://medium.com/@nikjohn/facebooks-yarn-vs-npm-is-yarn-really-better-1890b3ea6515">Facebook’s Yarn vs npm — Is Yarn really better?</a></p><p>First of all, on the project folder, initialize the project by typing command yarn init (or npm init). You can press Enter for every question.</p><pre>$ cd my-project-folder<br>$<strong> yarn </strong>init</pre><pre>or with NPM:</pre><pre>$ cd my-project-folder<br>$<strong> npm </strong>init</pre><p>This command just generates package.json file that stores all the info about our project.</p><h3>The Boss Webpack and his band(le) 😎</h3><p>Important concept of development with React, or just development, is splitting a project into small pieces, components. You create set of functions or class that solves a specific problem — put it in a separate file, then just include where you need in the project. But browser don’t understand include or import commands, it just get files and execute them one by one.</p><p>Here comes the <a href="https://webpack.js.org/"><strong>Webpack</strong></a>. It gets every module your application needs, then packages all of those modules into one <em>bundle file </em>(if needed — few bundles). Installation:</p><pre>$ <strong>yarn</strong> add webpack --dev</pre><pre>or with NPM:</pre><pre>$ <strong>npm</strong> install webpack --save-dev</pre><blockquote>Flag --dev (--save-dev) says that this dependency will be saved in devDependencies in package.json and required only in development.</blockquote><p>Then, on the project folder, create configuration file. By default Webpack will be looking for webpack.config.js :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29eb365d52b7fe9c534320b3a10f14ed/href">https://medium.com/media/29eb365d52b7fe9c534320b3a10f14ed/href</a></iframe><p>Property entry tells to bundle all assets from src/index.js together. The output property tells Webpack that final bundle.js file should be saved into dist folder.</p><blockquote>The path.resolve() method resolves a sequence of paths into an absolute path.</blockquote><p>On the next step add one necessary script to the package file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d5e853c4593b991fa4ebdeacbde591f/href">https://medium.com/media/4d5e853c4593b991fa4ebdeacbde591f/href</a></iframe><p>We need it because there is not webpack command globally, but when run yarn start (npm start), Node takes local installation of Webpack.</p><p>Create two files in src directory, where one import function from another:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa44ab04148f043c0ae368cbe1c32b4a/href">https://medium.com/media/aa44ab04148f043c0ae368cbe1c32b4a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/074fadab7ef051551fa3ff7d630118ad/href">https://medium.com/media/074fadab7ef051551fa3ff7d630118ad/href</a></iframe><p>Now, create index.html file (in dist directory) and include bundle file inside body tags and run yarn start:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28e6773ea99df8a6d8bb7d23181b2baa/href">https://medium.com/media/28e6773ea99df8a6d8bb7d23181b2baa/href</a></iframe><p>If you look, you may notice that bundle.js file appeared 👽 in dist directory. Open the page with browser and you should see — Hello WEBPACK. Profit! It works.</p><p>Moving on. ⏬</p><h3>Only JavaScript is not enough</h3><p>The main React development “tradition” is the use of <em>ES6</em> and HTML-like syntax (<em>JSX</em>) in JavaScript (JS) code. It’s time to move on to the new JS standard.</p><ul><li><strong>ES6 </strong>(EcmaScript 6) is a standard of JavaScript that gives a lot of new <a href="https://github.com/lukehoban/es6features">AWESOME features!!</a>.</li><li><a href="https://facebook.github.io/react/docs/introducing-jsx.html"><strong>JSX</strong></a><strong> </strong>is just a syntactic sugar for the React.createElement function.</li></ul><blockquote>ES6, because it’s the 6th Edition of JavaScript, but officially, the name is ECMAScript 2015 Language (ES2015).</blockquote><p>For example, JSX code:</p><pre>&lt;div className=”sidebar”&gt;&lt;/div&gt;</pre><p>compiles into:</p><pre>React.createElement( ‘div’, {className: ‘sidebar’}, null )</pre><p>Isn’t JSX easier to read? Definitely! But we need somehow transform JSX and ES6 into “old” JavaScript which understand all browsers (because only new browsers know about ES6). 🤔</p><h4>Babel</h4><p>And there is a solution — <a href="https://babeljs.io/"><em>Babel</em></a>. But it doesn’t have ES2015 and JSX compilers by default, Babel has been split into multiple packages:</p><ul><li><strong>babel-core </strong>— ️the main package, contains JavaScript parser, utilities and helper functions. ⚙</li><li><strong>babel-preset-es2015 </strong>— “chief worker” that compiles ES6 into ES5.</li><li><strong>babel-preset-react </strong>— transforms JSX into JS.</li></ul><pre>$<strong> yarn</strong> add babel-core babel-preset-es2015 babel-preset-react --dev</pre><p>But even after installing a presets, you need to tell Babel to use them by creating a .babelrc file in your project directory:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d6ebfb0c17791ebe9d90f397d5a3c88/href">https://medium.com/media/9d6ebfb0c17791ebe9d90f397d5a3c88/href</a></iframe><p>What do we need next? We need tell the Webpack to somehow transform code, using Babel. For this Webpack uses the concept of loaders. Loaders are transformations that are applied on the source code of a module. They allow you to pre-process files as you import or “load” them.</p><pre>$<strong> yarn</strong> add babel-loader --dev</pre><p>Now there are all the dependencies that we need to make Babel works. 🎉 We should add babel loader to configuration. It will be using babel-core with presets that we installed before for transformations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4dc72372905599d243d57484cf545ca/href">https://medium.com/media/a4dc72372905599d243d57484cf545ca/href</a></iframe><p>All other loaders can be added to the rules property array. Here Webpack watches all .js and .jsx files, imported in the app (except those in node_modules folder), and loads them using Babel.</p><h4>Webpack Dev Server</h4><p>But it’s not very convenient every time open index.html manually in browser. And Webpack comes to the rescue again 😉. What we need is called webpack-dev-server:</p><pre>$<strong> yarn</strong> add webpack-dev-server --dev</pre><p>The webpack-dev-server provides us with a simple web server and the ability to use live reloading. We just need to change our start script in package.json in order to work with webpack-dev-server:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/071bd15f27f04bf8d6e684c3347ec091/href">https://medium.com/media/071bd15f27f04bf8d6e684c3347ec091/href</a></iframe><p>The next step we should tell the server where to find files to serve:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bbd9a14d67b6b8388aa46388765339f/href">https://medium.com/media/3bbd9a14d67b6b8388aa46388765339f/href</a></iframe><p>Now, if you start server (yarn start), open the page (<a href="http://localhost:8080/">localhost:8080</a>) and change any file, browser will reload the page. Great! 🕺</p><h4>CSS loader</h4><p>The core philosophy of Webpack — <strong>everything is a module</strong>, CSS and images can also be modules! So, let’s add possibility to import CSS files as modules in our project. Two loaders needed for this:</p><ul><li><strong>css-loader</strong> —takes a CSS file and returns the CSS string with resolved imports and url(https://medium.com/...).</li><li><strong>style-loader</strong> — takes CSS string from css-loader and actually inserts it into the page.</li></ul><pre>$<strong> yarn</strong> add css-loader style-loader --dev</pre><p>The final Webpack config file should look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1944de23233579191cf5ea64a3a8978a/href">https://medium.com/media/1944de23233579191cf5ea64a3a8978a/href</a></iframe><h4>Actually React</h4><p>The last step for this part of article is installing actually React. As Babel, it consist of a few packages:</p><pre><strong>yarn</strong> add react react-dom</pre><p>Do not worry, react-dom is needed to manipulate the DOM and will be only used to render entry (root) React element. Change index.js (you can now remove upper.js file) and run yarn start:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3087dad873127740235a44c1553d2494/href">https://medium.com/media/3087dad873127740235a44c1553d2494/href</a></iframe><p>Open the page and you should see welcome text and input. Try to change input and welcome text will be also changing 😱. How this code works and how to create simple <a href="https://enkot.github.io/Minimal-React/">project management app</a> will be explained in the next part.</p><p>That`s it! I hope this article was useful.😃 🎓</p><p><em>If you like this post, please 👏 (40-50 times) for it on Medium and share it on Twitter, Facebook.</em></p><h4>The part 2 is coming soon…</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb9ecaef5094" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/its-easy-setting-up-react-and-webpack-eb9ecaef5094">Step by Step! React, Redux and Webpack</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>