<?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 Paquitosoftware on Medium]]></title>
        <description><![CDATA[Stories by Paquitosoftware on Medium]]></description>
        <link>https://medium.com/@paquitosoftware?source=rss-bde70de9f55------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4lvYF63-lLHh0bPJK4WnXg.png</url>
            <title>Stories by Paquitosoftware on Medium</title>
            <link>https://medium.com/@paquitosoftware?source=rss-bde70de9f55------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:00:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@paquitosoftware/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[Responsive rendering]]></title>
            <link>https://paquitosoftware.medium.com/responsive-rendering-262a975a956f?source=rss-bde70de9f55------2</link>
            <guid isPermaLink="false">https://medium.com/p/262a975a956f</guid>
            <category><![CDATA[responsive-design]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[code-splitting]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Paquitosoftware]]></dc:creator>
            <pubDate>Sat, 19 Dec 2020 14:21:30 GMT</pubDate>
            <atom:updated>2020-12-19T14:21:30.913Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NOCLxS0V1PDeMeO2znyWcQ.png" /><figcaption>Image of different devices (mobile, tablet and desktop)</figcaption></figure><p>Responsive design is almost a mandatory requirement for every web project you have to handle these days and most of us use this mobile-first approach where we begin designing for small devices (because most of your visits will come from them) and then adapt that same design for bigger screen resolutions.</p><p>When you are implementing these designs you deal with all those needs in terms of html markup and CSS so you are able to assemble a single source file to support the view in the different resolutions.<br>However, there are times where you may require a quite different design for different devices, such as <em>mobile </em>and <em>desktop</em>.<br>When I get to this point, I prefer to have separate source files for these alternative views so I can:</p><ul><li>Focus on the subtleties of each design</li><li>Focus the CSS styles for each one with their requirements</li><li>Make each one <strong>easier to reason about and mantain</strong></li><li><strong>Render only what is needed in each situation</strong></li></ul><p>Last week I was following this <a href="https://www.youtube.com/playlist?list=PL7CcGwsqRpSM3w9BT_21tUU8JN2SnyckR">fantastic tutorial</a> by <a href="https://twitter.com/adamwathan"><em>Adam Wathan</em></a><em> on </em><a href="https://tailwindcss.com/">TailwindCSS</a> (he’s the creator) and got to this situation where one of the components that has to be built have different requirements for small and big screens.</p><p>We had this profile button you can click on (in desktop version) to display different links regarding user actions. Also it had some olverlay behavior.</p><p>Here is the button with no interaction by the user:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c3r60PFZ6YowIyTHAUpJ7g.png" /><figcaption>Profile button (closed)</figcaption></figure><p>And here it is when the user clicks on it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AAyZfTilZkOXNZEvHtZlqg.png" /><figcaption>Profile button (opened)</figcaption></figure><p>Now let’s see the design for small resolution screens.<br>In this case, the profile button is hidden by default and it’s only shown when the user opens the links menu:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*o1gK6NtfsUM_QwAKJYZJIg.png" /><figcaption>Navigation menu opened in mobile</figcaption></figure><p>You can for sure have just one component that adapts the HTML markup, styles and behavior for both contexts, but this is just one of those situations where I prefer to have separate ones.</p><p>So, let’s jump to the (<strong>React</strong>) code…</p><p>This is the component for big screens:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e51ae134f57c6ed3a577353dcefe587f/href">https://medium.com/media/e51ae134f57c6ed3a577353dcefe587f/href</a></iframe><p>As you can see, there is the icon button, an overlay and the links.<br>Links and overlay are hidden by default and they are displayed only when the user click on the icon.<br>There is also code for <em>closing</em> the menu when pressing the <em>Escape</em> key.</p><p>Now let’s move to the small resolutions version of this component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e68245d8909e1ef05e7d9b0afef5ec9/href">https://medium.com/media/9e68245d8909e1ef05e7d9b0afef5ec9/href</a></iframe><p>This is a smaller component as it doesn’t need the overlay and links behavior.<br>Also, we are rendering the user name alongside the user icon.</p><p>Not only do I find easier to mantain these two components, but I am also able to render only what it’s required.<br>I mean, in traditional responsive implementation we normally hide those elements that are not mean to be shown in one specific resolution, but what if we do not render them at all? (little performance improvement).</p><p>So, the key for using this pattern is that consumers of this component do not know that there are different versions of it.<br>Here you can see how both these components are used in the context of the header of this fictional website.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a95c30f00b5b3807f4e7862fb03e5cdd/href">https://medium.com/media/a95c30f00b5b3807f4e7862fb03e5cdd/href</a></iframe><p>We’re just adding this <strong>AccountDropdown</strong> (maybe not the best name, I know…) in the <em>nav </em>element.</p><p>So, if we have two components, how do we export just one <em>interface</em> to the consumers?</p><p>We have this <em>interface/proxy</em> component which is the one that exports the specific implementation based on the current user’s screen resolution.<br>This is what I’m talking about:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d38d3f19624823bfb5ec41ed6fb3b637/href">https://medium.com/media/d38d3f19624823bfb5ec41ed6fb3b637/href</a></iframe><p>This is the component we’re importing from the website <em>header</em>.<br>As you can see, the <em>magic</em> is happening at another utility component called <strong>ResponsiveComponent</strong>.<br>We’re feeding this component with loaders for alternative <em>AccountDropdown</em> based on breakpoints.<br>We’re passing loaders instead of the components so we can <em>lazy-load</em> them and save us from downloading the version we’re not using on the browser.</p><p>Ok, let’s move to the <em>ResponsiveComponent</em> to check its implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f881991bcdcd98eaeaa1b50e1bd19c7/href">https://medium.com/media/4f881991bcdcd98eaeaa1b50e1bd19c7/href</a></iframe><p>As I was playing with <a href="https://tailwindcss.com/">TailwindCSS</a> I borrowed their breakpoints.<br>The gist here is that you can provide different versions for every screen resolution you’re dealing with in your project (regarding your breakpoints).<br>You don’t need to provide all of them. In this case I’m just passing a version for the smallest resolution and another one for the rest.<br><em>ResponsiveComponent</em> will use the smallest version avialable, based on the screen resolution and the <em>configured breakpoints</em>.</p><p>Note we’re using <a href="https://reactjs.org/docs/react-api.html#reactsuspense">Suspense</a> here to lazy-load this component and we can provide a Loading component to render while the actual one is being loaded.</p><p>Also notice how we’re using this <strong>selectResponsiveComponent</strong> function from a <em>custom hook</em> imported from a <strong>ResponsiveProvider </strong>to decide which specific component version of AccountDropdown we must use.</p><p>The thing is that we need somewhere to calculate and hold the state for the current screen resolution so we can later use that information when it’s time to pick an alternative resolution rendering component.<br>Also, we need to consider the screen resolution changes to update that state.</p><p>So, here is the code for that <strong>ResponsiveProvider</strong> where we implement a <em>React context</em> to manage all this stuff and also export the <em>custom hook</em> with required function utilities:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4d02b0df9d2ec512b90326fc25a5e86/href">https://medium.com/media/e4d02b0df9d2ec512b90326fc25a5e86/href</a></iframe><p>Here we’re first calculating the current breakpoint based on user browser resolution and then keeping it up to date if the browser window’s size changes.<br>We select the smallest breakpoint available.</p><h3>Summary</h3><p>I know we should aim for single component responsive design implementations but, at least for me, there are situations where having different components for each version is more convenient.</p><p>Here I explained the pattern I prefer using in these kind of scenarios where I try to hide the rest of the application if these components exist at all and be flexible enough to deal with the different breakpoints I’m using in the project.</p><p>You can find the whole code in <a href="https://github.com/PaquitoSoft/tailwindcss-tutorial/">this repo</a> where you might be able to better understand how all the pieces work together.</p><p>I was thinking in publishing the <strong>ResponsiveProvider</strong> and <strong>ResponsiveComponent </strong>as a NPM package for others to use but I’m not sure if there are many people out there that share this <em>responsive rendering</em> perspective with me.</p><p>Let me know your thoughts on this one.</p><p>Thanks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=262a975a956f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ReactJS Hooks: I’ve been waiting for you]]></title>
            <link>https://paquitosoftware.medium.com/reactjs-hooks-ive-been-waiting-for-you-8e5de3c8002?source=rss-bde70de9f55------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e5de3c8002</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Paquitosoftware]]></dc:creator>
            <pubDate>Sat, 03 Nov 2018 16:07:32 GMT</pubDate>
            <atom:updated>2018-11-03T16:07:32.650Z</atom:updated>
            <content:encoded><![CDATA[<p>I have to admit that I was dissapointed when React team decided to ditch mixins from their API.<br>Yes, I <a href="https://reactjs.org/blog/2016/07/13/mixins-considered-harmful.html">understand the motivation</a> and I also know it was for the better, but I didn’t like the alternatives: <a href="https://reactjs.org/docs/higher-order-components.html">HOCs</a> (at first) and <a href="https://reactjs.org/docs/render-props.html">render props components</a> (later).</p><p>Both the alternatives were clever solutions to the <em>share logic</em> problem, but they were no elegant for me. Every time I used them I had that feeling of writing the best of the worst solutions but I didn’t could came up with a better one.</p><p>Now, with the new <a href="https://reactjs.org/docs/hooks-intro.html">Hooks API</a> <strong>proposal</strong>, I finally find the elegant solution I was eager to use.</p><p><strong>IMPORTANT: Remember that this is an experimental feature yet. It’s NOT current part of the official API and we have to wait before using it in real projects.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XBUxsU0iDM4KdnMRyssHZw.jpeg" /><figcaption>Hooks are coming…</figcaption></figure><p>So, I’m really excited about this new API and the more I read about it and the more I experiment with it, the more I love it.</p><p>I want to show here some examples about how to use it and how them compare to the other <em>traditional</em> ways of sharing logic between components.</p><p>We have this simple APP to browse some podcasts that I call <strong>Podcaster</strong>.</p><p>You can find all the code for the application in <a href="https://github.com/PaquitoSoft/podcaster-react-hooks">this GitHub repository</a>.</p><p>We have three pages in this application: home, podcast detail and episode detail.<br>Each one must load some external data to can be shown so I wanted to extract that behavior logic to a shared <em>place</em>.</p><p>Moreover, the home page has a simple filtering input element so the users can narrow down the list shown.</p><p>Let’s fitst implement this with a <strong>High Order Component</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da3f3390063f6bfed240117316b4b31d/href">https://medium.com/media/da3f3390063f6bfed240117316b4b31d/href</a></iframe><p>And this is how we use that HOC from the home page view:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72a2e2e8a2dd0b66d7b125d4364d0899/href">https://medium.com/media/72a2e2e8a2dd0b66d7b125d4364d0899/href</a></iframe><p>So, in our page component we rely on some new props (<em>isLoading</em> and <em>externalData</em>) that the HOC provides.</p><p>From the point of view of this page component I guess it’s easy to reason about it as we wrap it with another one and kind of <em>earcn </em>its behavior (it’s composition not inheritance).</p><p>My <em>problem</em> here is that the HOC doesn’t feel natural. We’re writing an (visual) component that only holds behavior and we have to subscribe to different <em>lifecycle callbacks</em> in the context of the wrapped component.</p><p>In terms on LOC, we have 51 (HOC) + 56 (page component) = <strong>107</strong>.</p><p>Let’s move on and try the render <strong>prop component</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b67a77e0fd2698a8298123afe64960f1/href">https://medium.com/media/b67a77e0fd2698a8298123afe64960f1/href</a></iframe><p>And this is how we use that component from the home page view:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/677183359365d7b319475c978e6edac8/href">https://medium.com/media/677183359365d7b319475c978e6edac8/href</a></iframe><p>Render prop components were the next step in the sharing logic evolution after the HOC but, as you can see, from the point of view of its implementation, they are almost identical.</p><p>I guess this is a more compositional way but I always found it <em>ugly</em>. I don’t like the code I have to write into my <em>render</em> components: there is more indentation and it’s slightly more complicated to understand.</p><p>In terms on LOC, we have 53 (render prop component) + 60 (page component) = <strong>113</strong>.</p><p>Finally, let’s see how we implement this with the new Hooks API.<br>First, this is the custom hook:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/869ae5559f10640fe2a64ac699d9656d/href">https://medium.com/media/869ae5559f10640fe2a64ac699d9656d/href</a></iframe><p>And this is how we use that custom hook in the home page view:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d88ff06231d641a4718f21e51ad1849/href">https://medium.com/media/7d88ff06231d641a4718f21e51ad1849/href</a></iframe><p>Our custom hook is using several of the <a href="https://reactjs.org/docs/hooks-reference.html">built-in hooks</a> published in the new React API.</p><p>The page component is quite different because, as we are also using hooks in it to manage state, we don’t need it to be a class anymore (Hooks can’t be used in class components).</p><p>For me, this is a more natural approach.<br>We have a render method and we can import behaviour in it which is very easy to understand.</p><p>Maybe some will find this too imperative, but I feel like it’s super clear what the render method uses.</p><p>In terms on LOC, we have 36 (custom hook) + 38 (page component) = <strong>74</strong>.</p><p>Ok, thanks for getting this far :)</p><p>As I said in the beginning, I’m very excited with the new Hooks API because it allows me to write so much cleaner code that is <strong>easier to understand</strong> and, thus, <strong>easier to mantain</strong>.</p><p>This is not the <em>silver bullet</em> that will solve all your problems when developing new solutions with ReactJS, but I feel it will help a lot.</p><p>There are some questions remainig about how they will evolve into the final release but I hope it doesn’t take too long.</p><p>Thank you very much for reading this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e5de3c8002" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JS: Let’s try the future today (extra bonus)]]></title>
            <link>https://medium.com/hackernoon/js-lets-try-the-future-today-extra-bonus-43e9e1a8480a?source=rss-bde70de9f55------2</link>
            <guid isPermaLink="false">https://medium.com/p/43e9e1a8480a</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[vanillajs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[safari]]></category>
            <dc:creator><![CDATA[Paquitosoftware]]></dc:creator>
            <pubDate>Sat, 21 Oct 2017 17:21:43 GMT</pubDate>
            <atom:updated>2017-10-22T04:33:35.913Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*x4_wH2ZGei2YbqmNpv7oHg.jpeg" /></figure><p><a href="https://hackernoon.com/js-lets-try-the-future-today-ae74f3fbe656"><strong>In a previous article</strong></a> I showed a way to implement a basic and simple application with no third party dependencies or build tools, just using all the new goodies have and the latests browsers have already implemented.</p><p>There’s an extra feature we can add to that application with little effort but, currently it only works in Safari, as it is the only browser which have implemented then <a href="https://github.com/tc39/proposal-dynamic-import"><strong>dynamic import</strong></a> API.</p><p>With this API, we can defer the load of parts of the application code so the browser does not download it upfront.</p><p>What I want to implement now is the ability for the application to only download core code and current view code on page load so, if we load the <em>home</em> view (the one with the podcasts list), no code for the podcast detail view nor episode detail view are pulled by the browser.</p><p>So, we only have to change two files in our application to achieve this new feature.</p><p>(<em>Remember yo can see the complete code of the application </em><a href="https://github.com/PaquitoSoft/podcaster-vanilla-components"><strong><em>here</em></strong></a><strong><em>. </em></strong><em>There’s a branch in the repository called </em><strong><em>async-routing</em></strong><em> with this feature implemented</em>).</p><p>The strategy is to add an new step in the router navigation function so, before it renders the new page, it fetches the page-level component it needs to render.</p><p>I first need to change the routing configuration file so it doesn’t import the page-level components directly. Instead, I just set the relative path to the component (in this case, relative to the <strong>components</strong> folder):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Khr9PZAK9mPRMhmSGWE4PA.png" /><figcaption>Changes to routing configuration file</figcaption></figure><p>The only other thing I have to change is the router component so it grabs the corresponding file before trying to navigate.<br>Here is the <em>diff</em> for this file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lf7QCj_GVz9fgsKnAFViTw.png" /><figcaption>Changes to router component file</figcaption></figure><p>So, the important line is the one with the <strong>import</strong> call. This will tell the browser to fetch the file we’ve configured in the routing config (it’ll only do it once).<br>The import function returns a <a href="http://devdocs.io/javascript/global_objects/promise">Promise</a> which is resolved when the browser have loaded the required module file (and all its dependencies).</p><p>And, that’s it.<br>Just a few more lines and we have implemented <strong>code splitting</strong> to our application.</p><p>Keep in mind that currently only Safari supports dynamic imports (Chrome will support it in its next version, v63).<br>More status info: <a href="https://www.chromestatus.com/features/5684934484164608">https://www.chromestatus.com/features/5684934484164608</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43e9e1a8480a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/js-lets-try-the-future-today-extra-bonus-43e9e1a8480a">JS: Let’s try the future today (extra bonus)</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JS: Let’s try the future today]]></title>
            <link>https://medium.com/hackernoon/js-lets-try-the-future-today-ae74f3fbe656?source=rss-bde70de9f55------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae74f3fbe656</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[vanillajs]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[js]]></category>
            <dc:creator><![CDATA[Paquitosoftware]]></dc:creator>
            <pubDate>Mon, 02 Oct 2017 21:33:02 GMT</pubDate>
            <atom:updated>2017-10-21T17:25:20.951Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a simple exercise where I try to build a <strong>basic</strong> website (SPA) without any third party dependency or build tool but assuming we were in a world where all browsers have implemented most of the modern proposed Javascript APIs.</p><p>This is what we’ll be building: (<a href="http://podcaster-vanilla-components.surge.sh/">demo here</a>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l3uG3I-Dyxujk9uu_hpZoQ.png" /><figcaption>Podcaster home page</figcaption></figure><p>A simple website to look for music podcasts, list their episodes and listen to them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mdjJQuRUx3c-gKEKFQhQTw.png" /><figcaption>Podcast detail page</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8X3fPD1ENdUUfc0Q2rshaw.png" /><figcaption>Episode detail page</figcaption></figure><p>The main idea is to take a component based approach, so, with that in mind, this is the structure I’ll be using for the code:</p><pre>&lt;<em>root</em>&gt;<br> + index.html<br> + src<br>   - styles<br>   - js<br>     * <strong>api</strong> (here is the code to access podcasts services)<br>     * <strong>components</strong> (here are the views and partials for the UI)<br>     * <strong>config</strong> (here is the routing configuration)<br>     * <strong>plugins</strong> (support code)</pre><p><strong>You can get the final code</strong> <a href="https://github.com/PaquitoSoft/podcaster-vanilla-components"><strong><em>here</em></strong></a>.</p><p>The first goodie I’ll be using is <a href="https://www.chromestatus.com/features/5365692190687232">Javascript Modules loading support</a> in modern browsers (Chrome and Safari).</p><p>So, the HTML file would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83bf1942611c9e48f4b873a631bb565d/href">https://medium.com/media/83bf1942611c9e48f4b873a631bb565d/href</a></iframe><p>The important part here is the <strong>type</strong> attribute for the <em>script</em> tag.<br>Using the “<strong>module</strong>” value you’re telling the browser to use <a href="http://2ality.com/2014/09/es6-modules-final.html">ES2015 javascript modules</a> syntax with its <em>import/export</em> APIs.</p><p>In the markup, we only set a root entry point where we will <em>mount</em> our application from the main file.</p><h4>Application main file</h4><p>That main file, inserts the application layout (a header with a spinner we’ll use for loading state) and delegates main content rendering to the application router:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63216c6ba2a29b4dd948263f27179926/href">https://medium.com/media/63216c6ba2a29b4dd948263f27179926/href</a></iframe><p>We just create a new <em>html</em> element with the layout and setup the client-side router with the routing configuration, the root element where it should render the main content (based on the URL) and a listener so we can react to loading events (transition across pages when data is not cached).</p><h4>Router</h4><p>So, client-side routing. Many would automatically look for a third party library because it seems that this is a complex area, but the truth is, if you just need to handle simple navigations, building a router from the scratch is not that difficult. It’s just sync your URL with a component.<br>Regarding to data needed by the rendering component, you can setup the component to load it or you can have the router load it and just pass it to the component. I prefer the second way because I don’t want the new page to begin rendering until I have its data and I also don’t want to handle a <em>loading</em> state for every page (if you create helper component for this, I think its better just to use the <em>router</em>).</p><p>Here is the application router:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7854143462445d648e33ff5602e9adee/href">https://medium.com/media/7854143462445d648e33ff5602e9adee/href</a></iframe><p>It is a simple one, but it’s enough to begin with.<br>I used this approach in some other projects with a slightly more complex router and I usually create another file for <em>popstate</em> proper handling, and to export some <em>navTo</em> and <em>redirectTo</em> helper functions.</p><p>So this is what we do:</p><ul><li>Listen to click events on links in the document</li><li>Listen to external changes in the URL (example: <em>back</em> and <em>forward</em> buttons from the browser)</li><li>Fire a first change URL handling when the page loads</li></ul><p>And when we need to load a new page:</p><ul><li>Based on the URL we want to navigate to, find the right router configuration</li><li>Once we have that one, we extract the parameters the data loader might need from the URL</li><li>Call the loader to fetch required data</li><li>Update router internal state: new view component and new data</li><li>Re-render the router main element with the new state by instantiating a new component with the new data and call its <strong>render</strong> function, which returns an <em>HTMLElement</em>.</li><li>Restore scroll position</li></ul><p>There’s also the execution of the callback to show the app loading indicator.</p><h4>Home page</h4><p>Ok, so next thing we need to learn about are the <em>pages</em> (home, podcast-detail and episode-detail).<br>We’ve just seen that <em>pages</em> might have a <strong>constructor</strong> which accepts initial data and a <strong>render</strong> function which must return an <em>HTMLElement</em> with the expected content.</p><p>As all of the <em>pages</em> will have some common behavior, and we will need several instances of them, we’ll be creating a base class for them which every page will inherit from.</p><p>Our pages will have a touch from <a href="http://backbonejs.org/"><strong>Backbone</strong></a> and another one from <a href="https://reactjs.org/"><strong>React</strong></a>.<br>Let’s begin to take a look at the <em>HomePage</em> view.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/079a6b1ed12083babeebc14ec3281586/href">https://medium.com/media/079a6b1ed12083babeebc14ec3281586/href</a></iframe><p>In the <em>constructor</em> of the page we receive the data from the <em>router</em> and call parent to store instance state. We will be using that state to re/render the view.</p><p>We also have a <em>getter</em> which returns a map of the DOM events the page must handle. The syntax is inspired by <a href="http://backbonejs.org/"><strong>Backbone</strong></a><strong>.</strong><br>The key for every entry has to parts: the first one is the name of the DOM event we want to listen to and the second part, the <em>selector</em> for “<em>the element we want to have the listener attached to”; </em>the second part is the function we want to associate with the event.<br>Actually, the event listeners are not bound to the elements represented by the selector, they are all bound to the root element for a view instance and there’s a delegation management in the <em>BasePage</em>.</p><p>The function used in the render process is the <strong>html</strong> one, which must return a string representing the HTML to visualize the page with its provided state.<br>In the<em> router</em> we saw that we were generating the contents for the page calling a <em>render</em> function which returns an HTMLElement. That function is implemented in the <em>BasePage</em> class (we’re about to get there).</p><p>The last important thing to note is the static <strong>dataLoader</strong> function, which is used by the router to know how to load (asynchronously) the data this view requires.</p><h4>Base page class</h4><p>So far so good. Let’s take a look at the base class for every application page:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d8cf0b7164b9358eb5d66e5866711ad/href">https://medium.com/media/4d8cf0b7164b9358eb5d66e5866711ad/href</a></iframe><p>In the constructor we store the data received from the page class, create the root HTMLElement for this view and register the possible DOM event handlers the subclass might define.</p><p>Then, we only have a function to update instance state (which re-renders the page instance) and the render function for setting HTML string produced by the subclass in the root instance HTMLElement.</p><h4>Partials</h4><p>If you get back to the <em>HomePage</em> render code, you will notice that every podcast info is rendered using the <em>PodcastSummary</em> partial.</p><p>This kind of component is a simpler one because it does not need to handle navigation and don’t need dynamic state.<br>These ones are like <strong><em>stateless functional components</em></strong> in <a href="https://reactjs.org/"><strong>React</strong></a>. They just receive the data to render and return the corresponding HTML string.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/856f2d95a99f159cd5a01dd38f594c26/href">https://medium.com/media/856f2d95a99f159cd5a01dd38f594c26/href</a></iframe><h4>The rest of the code</h4><p>The code for the other pages are quite similar of what you’ve just seen in the <em>HomePage</em> class.<br>There are some other relevant files:</p><ul><li>/src/api/<strong>podcaster.js</strong> This file holds the AJAX calls to the iTunes podcast services as well as the requests to fetch the podcast RSS detail file.</li><li>/src/plugins/<strong>local-cache.js</strong> This file is a proxy for the local-storage API that allows to store values with a Time To Live meta-info.</li><li>/src/plugins/<strong>ajax.js</strong> This file is a thin wrapper over the fetch API to simplify its use.</li></ul><p>You can browse/download the <a href="https://github.com/PaquitoSoft/podcaster-vanilla-components">finished code for this pet project here</a> to get their details.</p><h4>Conclusion</h4><p>As you can see, with the latest browsers, you can implement a <strong>basic</strong> solution with no third party dependencies in a quite easy way.</p><p>I’m sure that, a year or two ahead, when all the browsers have this APIs implemented, there will be newer outstanding ones that will be only available in one or two of them, so we will still waiting for all the browsers to implement them, just as we are now. But, you know? That’s what make our lives entertaining.</p><p>If you got this far, I want to THANK you for giving me some of your time and I hope you found something useful in this article.</p><p><strong>Extra Bonus</strong>: I’ve written a follow-up article to show how easy can be to add <em>code splitting</em> to this basic application.<br>Check it out! <a href="https://medium.com/@paquitosoftware/js-lets-try-the-future-today-extra-bonus-43e9e1a8480a"><strong>JS: Let’s try the future today (extra bonus)</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae74f3fbe656" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/js-lets-try-the-future-today-ae74f3fbe656">JS: Let’s try the future today</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>