<?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 Dudu Popkhadze on Medium]]></title>
        <description><![CDATA[Stories by Dudu Popkhadze on Medium]]></description>
        <link>https://medium.com/@dudupopkhadze?source=rss-e1e334c6848f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*A-vbu5X7vHO2SAWijHNRUg.png</url>
            <title>Stories by Dudu Popkhadze on Medium</title>
            <link>https://medium.com/@dudupopkhadze?source=rss-e1e334c6848f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 10:52:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dudupopkhadze/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[One UI-Kit to rule them All]]></title>
            <link>https://levelup.gitconnected.com/one-ui-kit-to-rule-them-all-3d1ea24c9794?source=rss-e1e334c6848f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d1ea24c9794</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Dudu Popkhadze]]></dc:creator>
            <pubDate>Mon, 06 Mar 2023 06:56:16 GMT</pubDate>
            <atom:updated>2023-03-07T19:27:52.031Z</atom:updated>
            <content:encoded><![CDATA[<p>What if your react-native mobile app code grows as fast as a web application and you end up writing the same components twice (or more)?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u5f04kCp9QDHBGtSf2sgrQ.png" /></figure><h4>Problem</h4><p>So every engineer has seen how fast client-side code grows. Every week brings new features, bug fixes, and minor improvements.</p><p>At this age, every startup needs a mobile application and most likely designs of mobile and web apps are quite similar but not exactly. So at my job, we have these types of client apps</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*u-iKGDpWvIN_aKSyFPJhnA.png" /></figure><p>All of these applications have different layouts and designs but the building blocks were the same. The first thought that comes to mind after encountering this situation is to share styles somehow but soon you come to the realization that sharing just styles is just the same headache.</p><ul><li>While sharing styles you still need to implement components from scratch every time</li><li>If you decide to share components, you still have to implement them twice (or more) for the web and for the mobile</li><li>Takes more of the engineers&#39; time, you need someone who knows your web technology and someone with mobile development knowledge as well</li></ul><p>So what’s the solution?</p><h4>Solution</h4><p>While trying to find a solution we came across react-native-web, a kinda new tech that is highly used by Twitter and Uber, so since these tech giants are building their applications with it, it should be cool, right?</p><blockquote>React Native for Web is an accessible implementation of React Native’s Components and APIs that is interoperable with React DOM.</blockquote><p>what are the pros of rn-web?</p><ul><li>Support different devices and input modes, and render semantic tags.</li><li>Support gestures and multiple input modes (touch, mouse, keyboard).</li><li>Rely on scoped styles and automatic vendor prefixing.</li><li>Respond to element resize events.</li><li>Interoperates with existing React DOM components. Bundle only what you use.</li></ul><p>Basically, we write client-side code with API provided by react-native-web and then use it on mobile and the web, API integrations are fully taken care of by rn-web. At this point, everything sounds good, but there is still a problem, how do we share code in the project? We need a helper library.</p><h4>Tools</h4><p>When developing a client-side library, it’s crucial to be able to test it properly, for this we need some kind of tool that will help us with testing this design system, Storybook is perfect for this.</p><blockquote>Storybook is a frontend workshop for building UI components and pages in isolation. Thousands of teams use it for UI development, testing, and documentation. It’s open source and free.</blockquote><h4>Architecture</h4><p>First of all, What do we need to share between the projects?</p><ul><li>Basic UI elements like: Text, Button, Loader</li><li>Complex components built with basic UI elements</li><li>Fonts</li><li>Icons, Svgs, Illustrations</li></ul><h4>Implementation</h4><p>First of all, let’s create a react-storybook project managed with webpack</p><pre>mkdir optimum-uikit // create project folder<br><br>cd optimum-ukit // navigate to folder<br><br>npm init // init package.json <br><br>npm i react react-dom react-native react-native-web typescript // setup base packages<br><br>npx storybook init // init base storybook <br></pre><p>By default storybook uses webpack, to make react-native-web work we need to adjust .storybook/main.js, also let’s change the stories config so the storybook looks for them in the src folder</p><pre>module.exports = {<br>  &quot;stories&quot;: [<br>    &quot;../src/**/*.stories.mdx&quot;,<br>    &quot;../src/**/*.stories.@(js|jsx|ts|tsx)&quot;<br>  ],<br>  &quot;addons&quot;: [<br>    &quot;@storybook/addon-links&quot;,<br>    &quot;@storybook/addon-essentials&quot;,<br>    &quot;@storybook/addon-interactions&quot;<br>  ],<br>  &quot;framework&quot;: &quot;@storybook/react&quot;, <br>  webpackFinal: async (config, { configType }) =&gt; {<br>    config.resolve.alias[&#39;react-native$&#39;] = &#39;react-native-web&#39;<br><br>    return config<br>  },<br>  typescript: {<br>    check: true,<br>  }<br>}</pre><p>This config tells webpack to use code from react-native-web whenever it comes upon the code from react-native. Also, don’t forget to initialize tsconfig.json if you want to use Typescript.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*UGHt8FLJoSOAQ6nEfNwn5w.png" /></figure><p>At this point our project structure looks like this, let’s get rid of the stories folder and create the basic component with the story by ourselves. The component story captures the rendered state of a UI component. The stories are written in Component Story Format (CSF) — an ES6 modules-based standard — for writing component examples, after we create a story for a component, storybook will take care of rendering and displaying it.</p><pre>// src/Button/index.tsx<br><br>import React from &quot;react&quot;<br><br>interface ButtonProps {<br>  text: string<br>  onPress: () =&gt; void<br>}<br><br>export const Button = (props: ButtonProps) =&gt; {<br>  return &lt;&gt;&lt;/&gt;<br>}</pre><pre>// src/Button/index.stories.tsx<br><br>import React from &#39;react&#39;<br>import { Meta, Story } from &#39;@storybook/react&#39;<br>import { Button, ButtonProps } from &quot;.&quot;<br><br>const Template: Story&lt;ButtonProps&gt; = (args) =&gt; &lt;Button text={args.text} onPress={args.onPress}/&gt; <br><br>export const Default = Template.bind({})<br><br>Default.args = {<br>  text: &quot;Click me!&quot;,<br>  onPress: () =&gt; console.log(&quot;You clicked.&quot;)<br>}<br><br>const meta: Meta = {<br>  component: Button,<br>  title: &#39;Design System/Button&#39;<br>}<br><br>export default meta</pre><ul><li>Create <em>src/Button/index.tsx</em> file in the project. So here you see a basic react button component, we have a text prop for displaying on a button, and an onPress prop that must be fired after the user clicks.</li><li>Create <em>src/Button/index.stories.tsx</em> file. This is the most basic storybook story, just rendering our component and providing it with needed props. Meta helps storybook to render components properly and display the component’s name.</li><li>As we know, there are no &lt;div/&gt; &lt;button/&gt; and addEventListeners in react-native, so what do we do now? This is where react-native-web shines.</li></ul><pre>// src/Button/index.tsx<br><br>import React from &quot;react&quot;;<br>import { StyleSheet, TouchableOpacity, Text, View } from &quot;react-native&quot;;<br><br>export interface ButtonProps {<br>  text: string;<br>  onPress: () =&gt; void;<br>}<br><br>export const Button = (props: ButtonProps) =&gt; {<br>  return (<br>    &lt;TouchableOpacity onPress={props.onPress}&gt;<br>      &lt;View style={sheet.container}&gt;<br>        &lt;Text style={sheet.text}&gt;{props.text}&lt;/Text&gt;<br>      &lt;/View&gt;<br>    &lt;/TouchableOpacity&gt;<br>  );<br>};<br><br>const sheet = StyleSheet.create({<br>  container: {<br>    padding: 12,<br>    backgroundColor: &quot;blue&quot;,<br>    justifyContent: &quot;center&quot;,<br>    alignItems: &quot;center&quot;,<br>    maxWidth: 200,<br>    borderRadius: 20,<br>  },<br>  text: { color: &quot;white&quot;, fontSize: 18 },<br>});</pre><p>So what’s happening here? If you have ever used react-native you would be familiar with View, Text, TouchableOpacity components. Those tags are just the same as &lt;div/&gt;, &lt;span/&gt;, &lt;button/&gt; but only in the react-native world.</p><p>To sum up, we are writing code just like we would if we were building a mobile application but it works perfectly in the browser without any modification. This is the real power of react-native-web, it converts our View tag to div under the hood and maps all APIs accordingly. And here is the result :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uqUhKCw2VQltM4WppXe4sg.png" /></figure><p>But how does react-native-web do this? To answer that question first let’s take a look at HTML generated by our component</p><pre>&lt;div tabindex=&quot;0&quot; class=&quot;...styles&quot;&gt; // corresponds to &lt;TouchableOpacity/&gt;<br>  &lt;div class=&quot;...bunch of auto generated styles&quot;&gt; // corresponds to &lt;View/&gt;<br>    &lt;div dir=&quot;auto&quot; class=&quot;...styles&quot;&gt;Click me!&lt;/div&gt;// corresponds to &lt;Text/&gt;<br>  &lt;/div&gt;<br>&lt;/div&gt;<br></pre><p>As you see, we used &lt;Text/&gt; tag but got another &lt;div/&gt; in your HTML tree, Why? To understand this process better let’s check out a small piece of code ( not complete, I removed some parts for simplicity ) from the react-native-web repo which is responsible for rendering &lt;Text/&gt; tag</p><pre>// ref: https://github.com/necolas/react-native-web/blob/master/packages/react-native-web/src/exports/Text/index.js<br><br>let component = hasTextAncestor ? &#39;span&#39; : &#39;div&#39;;<br><br><br>if (onClick || onPress) {<br>  supportedProps.onClick = handleClick;<br>}<br><br>supportedProps.style = [<br>  numberOfLines != null &amp;&amp;<br>    numberOfLines &gt; 1 &amp;&amp; { WebkitLineClamp: numberOfLines },<br>  hasTextAncestor === true ? styles.textHasAncestor$raw : styles.text$raw,<br>  numberOfLines === 1 &amp;&amp; styles.textOneLine,<br>  numberOfLines != null &amp;&amp; numberOfLines &gt; 1 &amp;&amp; styles.textMultiLine,<br>  props.style,<br>  selectable === true &amp;&amp; styles.selectable,<br>  selectable === false &amp;&amp; styles.notSelectable,<br>  onPress &amp;&amp; styles.pressable<br>];<br><br>if (props.href != null) {<br>  component = &#39;a&#39;;<br>  if (hrefAttrs != null) {<br>    const { download, rel, target } = hrefAttrs;<br>    if (download != null) {<br>      supportedProps.download = download;<br>    }<br>    if (rel != null) {<br>      supportedProps.rel = rel;<br>    }<br>    if (typeof target === &#39;string&#39;) {<br>      supportedProps.target =<br>        target.charAt(0) !== &#39;_&#39; ? &#39;_&#39; + target : target;<br>    }<br>  }<br>}<br><br>const element = createElement(component, supportedProps, {<br>  writingDirection<br>});</pre><ul><li>RNW checks if the ancestor of the current component was &lt;Text/&gt; tag. If true → select &lt;span/&gt; as a tag for layout purposes, If false → select &lt;div/&gt; tag.</li><li>Adds onClick handler to supportedProps object</li><li>Adds styles to supportedProps</li><li>supportedProps object then is later on translated to DOM’s API by createDOMProps function</li><li>Then it looks at href prop, if it’s present RNW switches to &lt;a/&gt; tag and adds all supported fields for this element</li><li>After this RNW calls createElement function</li></ul><pre>// ref: https://github.com/necolas/react-native-web/blob/master/packages/react-native-web/src/exports/createElement/index.js#L15<br><br>const createElement = (component, props, options) =&gt; {<br>  // Use equivalent platform elements where possible.<br>  let accessibilityComponent;<br>  if (component &amp;&amp; component.constructor === String) {<br>    accessibilityComponent =<br>      AccessibilityUtil.propsToAccessibilityComponent(props);<br>  }<br>  const Component = accessibilityComponent || component;<br>  const domProps = createDOMProps(Component, props, options);<br><br>  const element = React.createElement(Component, domProps);<br>  <br>  return element;<br>};</pre><ul><li>createElement function adds accessibility stuff to the component, next translates provided props to DOM’s API standard and calls React.createElement at last</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*zUJyTMpFVRBzgMJm03oR1Q.png" /></figure><p>Finally, we got a picture like this, a single project that uses react-native-web for implementing components, but after the build, it gets distributed around projects on different platforms, on mobile, web, desktop, and maybe even somewhere else.</p><h4>Next Steps</h4><ul><li>Make this project into npm private package that can only be accessed with your team member’s tokens ( try out release-it npm package for making things easier)</li><li>Build separate builds for esm and cjs, some of your projects can be using different module systems, this is quite easy with tsc, you just have to have separate tsconfig files</li><li>When installing this ui-kit in the client app, you will need to adjust the bundler config a little bit, for example in the case of webpack you will need to add those two lines</li></ul><pre>resolve: {<br>  modules: [path.resolve(__dirname, &#39;src&#39;), &#39;node_modules&#39;],<br>    &#39;react-native$&#39;: &#39;react-native-web&#39;<br>  },<br>  extensions: [&#39;.web.js&#39;, &#39;.js&#39;]<br>}</pre><ul><li>Go ahead and try it out</li></ul><h4>Some Notes</h4><ul><li>Use StyleSheet.create wisely, it’s a very good tool that can narrow down your bundle size drastically</li><li>Use Platform.OS or Platform.pick({}) when dealing with platform-specific implementations</li><li>You can add two implementations for the same component. If you have Button/index.tsx and Button/indext.web.tsx .</li><li>If you use typescript in client apps, you can introduce a helper library for types, hooks, utils. This will help with normalizing component props.</li><li>If you don’t want to deal with react-native-svg for icons, Fontello is a great tool for generating font from your svg icons, after that, you can use expo-font ’s createIconSetFromFontello function.</li><li>Animations: you can make react-native-reanimated work with react-native-web but if you decide to do this, be ready for a rough journey, I would suggest keeping animation logic separate, maybe even splitting them into different files : animation.tsx and animation.web.tsx</li><li>Use <a href="https://nodejs.org/es/blog/npm/peer-dependencies/">peerDependencies</a> wisely, sometimes you will need platform-specific libraries and peer dependencies will come in handy</li><li>Spin up a preview page automatically when a new PR opens up on this repo, will help a lot with reviewing</li></ul><p>Resources for better understanding this project</p><ul><li><a href="https://necolas.github.io/react-native-web/">React Native Web</a></li><li><a href="https://storybook.js.org/">Storybook</a></li><li><a href="https://github.com/release-it/release-it">release-it</a></li><li><a href="https://github.com/dudupopkhadze/optimum-uikit">Repo</a> with the code from above</li></ul><h4>Collaborators</h4><ul><li><a href="https://github.com/notgiorgi">notgiorgi</a></li><li><a href="https://github.com/prosperi">pros</a>peri</li><li><a href="https://github.com/lberia">lberia</a></li></ul><p>This kind of tech is highly used within <a href="https://gopeer.org/">GoPeer</a> , go check us out.</p><p>For any questions here are my <a href="https://twitter.com/dudupopkhadze">twitte</a>r and <a href="https://github.com/">github</a>.</p><h3>Level Up Coding</h3><p>Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://levelup.gitconnected.com/?utm_source=pub&amp;utm_medium=post">Level Up Coding publication</a></li><li>💰 Free coding interview course ⇒ <a href="https://skilled.dev/?utm_source=luc&amp;utm_medium=article">View Course</a></li><li>🔔 Follow us: <a href="https://twitter.com/gitconnected">Twitter</a> | <a href="https://www.linkedin.com/company/gitconnected">LinkedIn</a> | <a href="https://newsletter.levelup.dev/">Newsletter</a></li></ul><p>🚀👉 <a href="https://jobs.levelup.dev/talent/welcome?referral=true"><strong>Join the Level Up talent collective and find an amazing job</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d1ea24c9794" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/one-ui-kit-to-rule-them-all-3d1ea24c9794">One UI-Kit to rule them All</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NodeJS: Modify Running Server Behaviour From Outside]]></title>
            <link>https://levelup.gitconnected.com/nodejs-modify-running-server-behaviour-from-outside-445cee1f02b7?source=rss-e1e334c6848f------2</link>
            <guid isPermaLink="false">https://medium.com/p/445cee1f02b7</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Dudu Popkhadze]]></dc:creator>
            <pubDate>Wed, 17 Nov 2021 07:57:56 GMT</pubDate>
            <atom:updated>2021-11-17T13:55:45.098Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you heard about Chrome DevTools Protocol?!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*WewaZjfHc_PNt-r8.jpg" /></figure><h3>DevTools</h3><p>Every engineer I know has used DevTools at least once in his career. DevTools is a very useful tool that can make debugging your NodeJS app much easier, of course, if you know how to properly use it.</p><p>Most of us take this tool for granted, with one mouse click we open the DevTools window and start doing the magic, but How is it that we can debug the local NodeJS process inside our browser? how is it implemented? and the main question: How can we implement our custom Debugger?</p><p>Answering these questions will give us the power to reach our goal. 👀</p><h3>Goal</h3><p>Imagine we have an Express server running on our local machine and in one moment we decide to drop all incoming PATCH requests, of course, we can stop our server → change code → run server again. But can we do this without restarting/stoping our server? Let’s see</p><h3>Chrome DevTools Protocol ( CDP )</h3><blockquote>The <strong>Chrome DevTools Protocol</strong> allows for tools to instrument, inspect, debug and profile Chromium, Chrome and other Blink-based browsers. Many existing projects currently use the protocol. The <a href="https://developers.google.com/web/tools/chrome-devtools/">Chrome DevTools</a> uses this protocol and the team maintains its API.</blockquote><p>Basically, CDP is a protocol that helps us to connect with our apps and debug them. CDP is like any other protocol we use on daily basis, so since we are engineers we can build/do anything with it.</p><p>CDP has pretty well-written <a href="https://chromedevtools.github.io/devtools-protocol/">documentation</a>, many useful and popular tools use it under the hood. For example: <a href="https://github.com/puppeteer/puppeteer">popputeer</a>, <a href="https://github.com/GoogleChrome/lighthouse">lighthous</a>, <a href="https://github.com/Microsoft/vscode-chrome-debug/">vs-code debugger</a> , <a href="https://github.com/microsoft/playwright">playwright</a> and many more.</p><h3>Implementation</h3><p>Firstly let’s bootstrap a simple Hello World app with ExpressJS and log incoming request’s methods</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe06364cfe6d755934740b91fbad9fe9/href">https://medium.com/media/fe06364cfe6d755934740b91fbad9fe9/href</a></iframe><p>With node app.js simple express server will start and it will be listening on 3000 port. Let’s throw some requests at it to prove that it works.</p><pre>Input<br>---&gt;<br>curl -s  --request PATCH &#39;<a href="http://localhost:3000/&#39;">http://localhost:3000/&#39;</a><br>curl -s  --request GET &#39;<a href="http://localhost:3000/&#39;">http://localhost:3000/&#39;</a><br>---&gt;</pre><pre>Server Output<br>---&gt;<br>Example app listening at <a href="http://localhost:3000/">http://localhost:3000</a><br>PATCH<br>GET <br>---&gt;</pre><p>After start-up, we need to tell our NodeJS app to listen for the debugging client (CDP connection in other words). For this we can start our app with --inspect switch or we can send SIGUSR1 signal and Node will handle it automatically.</p><p>Since we don’t want to stop our server, to send a signal to the process we need a PID, we can get it with lsof -i :3000 command, after that executing kill -SIGUSR1 {PID} will enable debug mode, our app now looks like this:</p><pre>---&gt;<br>Example app listening at <a href="http://localhost:3000/">http://localhost:3000</a><br>Debugger listening on ws://127.0.0.1:9229/a58914a2-86d7-4990-bdf9-53a43e5cb47d<br>For help, see: <a href="https://nodejs.org/en/docs/inspector">https://nodejs.org/en/docs/inspector</a><br>---&gt;</pre><p>Now we have a running server waiting for CDP connection at 9229 port (default CDP port for NodeJS), the only thing that remains is to write the client-side part.</p><p>Here is the CDP client code which we will be discussing line by line →</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5f87545ae7b6fd37f909a2a77bcca8c/href">https://medium.com/media/e5f87545ae7b6fd37f909a2a77bcca8c/href</a></iframe><h4>#1 Connect to the Process →</h4><p>Create CDP connection at NODE_DEFAULT_DEBUGGER_PORT</p><pre>const cdp = <em>await</em> CDP({ port: 9229 });</pre><h4>#2 Enable Runtime Evaluation →</h4><p>Enable Runtime domain which exposes JavaScript runtime by means of remote evaluation and mirror objects</p><pre><em>await</em> cdp.Runtime.enable();</pre><h4>#3 Getting Prototype of HTTP Server →</h4><p>With cdp.Runtime domain we can query any JS runtime object , result is returned as mirror object that exposes object type, string representation and unique identifier that can be used for further object reference.</p><p>Express apps all use http API under the hood, app.listen calls require(‘http’).createServer which creates new http.Server instance. Since every object has it’s prototype, let’s get our hands on http.Server.prototype</p><p>How can we do this? Lucky for us with Runtime.evaluate we can evaluate any code inisde the process we are connected to, so let’s look for our prototype</p><pre>const prototype = <em>await</em> cdp.Runtime.evaluate({<br>    // specify which prototype we want<br>    expression: &quot;require(&#39;http&#39;).Server.prototype&quot;,</pre><pre>    // this is required for &quot;require&quot; to work<br>    includeCommandLineAPI: true, <br>    <br>    // True we need a pointer to the prototype of http.Server in the <br>    // process heap, otherwise it would return JSON object.<br>    returnByValue: false,<br>});</pre><h4>#4 Getting Object Pointers →</h4><p>We have a pointer to http.Server prototype, now it’s time to get actual object mirrors with this prototype, Runtime.queryObjects returns a pointer to an array. This array contains the list of objects which have http.Server.prototype as a prototype.</p><pre>const listOfObjects = <em>await</em> cdp.Runtime.queryObjects({<br>    // specify prototype for objects<br>    prototypeObjectId: prototype.result.objectId,<br>});</pre><h4>#5 Getting Instance of the Server →</h4><p>We acquired all object pointers with http.Server.prototype , now we need actual instances of this objects in JS runtime which are initialized in our process. Runtime.getProperties returns an array that contains the list of pointers of the actual instances.</p><p>Since we have only one http server running, returned array will contain only one element. Now we have our desired instance at result[0].value.objectId</p><pre>const listOfInstances = <em>await</em> cdp.Runtime.getProperties({<br>    // specify objectId<br>    objectId: listOfObjects.objects.objectId,<br>});</pre><pre>// select first instance from result array<br>const serverInstance = listOfInstances.result[0].value.objectId;</pre><h4>#6 Fun Part Begins! →</h4><p>Our goal is to override listeners of http.Server , for this we will need to write some function which → remove this listeners from the instance → wraps them in other magic function which drops PATCH requests → adds this listeners back.</p><p>I defined this function in other cool_code.js file, otherwise i had to implement it in plain string and that’s something i hate more than debugging CSS. We will go through cool_code after we finish with inject.js . The code we want to be evalueted in our process is:</p><pre>var overRideListeners = require(`./injectable`).overRideListeners</pre><p>This defines a global function overRideListeners (implementation later) which will take care of hijacking HTTP requests.</p><pre><em>await</em> cdp.Runtime.evaluate({<br>    expression:<br>            `var overRideListeners = require(&#39;./cool_code&#39;)<br>                                               .overRideListeners`, <br>    includeCommandLineAPI: true,<br>    returnByValue: false,<br>});</pre><h4>#7 Overriding! →</h4><p>So we have a magic function declared inside the process, to reach our goal now we need to call this function on an instance of http.Server we acquired earlier. Surprisingly Runtime offers a method callFunctionOnwhich does exactly that.</p><pre><em>await</em> cdp.Runtime.callFunctionOn({<br>    objectId: serverInstance,<br>    functionDeclaration: &quot;function() { overRideListeners(this) }&quot;,</pre><pre>    returnByValue: true,<br>});</pre><h4>#8 Cleanup →</h4><p>At this point if we did everything correctly (Spoiler Alert: we did) our Express app should not be handling PATCH request from the network.</p><p>Only thing that remains is to see that cool_code implementation.</p><p>Also let’s be good engineers and close opened connection + release pointer we acquired.</p><pre><em>await</em> cdp.Runtime.releaseObject({ objectId: serverInstance });<br><em>await</em> cdp.close();</pre><h3>Cool Code</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6afcd581b886531907151cabf4f9388/href">https://medium.com/media/a6afcd581b886531907151cabf4f9388/href</a></iframe><p>For overriding http.Server listeners we need Shimmer , a npm library that does a bunch of the work necessary to wrap other methods in a wrapper you provide.</p><p>overRideListener step by step:</p><ul><li>Firstly we retrieve all listeners for request event and save them</li><li>Remove current listeners</li><li>Override each of them with Shimmer</li><li>Drop any PATCH request with status code 405</li><li>Register modified listeners back on request event</li></ul><p>This is all overRideListener does, yea I know it’s not that cool.</p><h3>Action</h3><p>In the first part of this article we sent SIGUSR1 signal to our process, this means that it’s still waiting for CDP connection, we have a client ready so let’s make it happen</p><pre>node inject.js // activate injection script</pre><p>This message should appear in server’s logs</p><pre>Debugger attached.</pre><p>This means probably we did everything right, it’s time to throw some requests at our server once more</p><pre>Input<br>---&gt;<br>curl -s  --request PATCH &#39;<a href="http://localhost:3000/&#39;">http://localhost:3000/&#39;</a><br>curl -s  --request PATCH &#39;<a href="http://localhost:3000/&#39;">http://localhost:3000/&#39;</a><br>curl -s  --request GET &#39;<a href="http://localhost:3000/&#39;">http://localhost:3000/&#39;</a><br>---&gt;</pre><pre>Server Output<br>---&gt;<br>Example app listening at <a href="http://localhost:3000/">http://localhost:3000</a><br>GET <br>---&gt;</pre><h3>Conclusion</h3><p>As you can see, after executing inject.js our process drops every incoming PATCH request from the network but is responding to GET request as it used to, this means we changed the behavior of running NodeJS application without stopping/restarting the process — Mission Accomplished.</p><p>CDP is a very cool tool that can make development much easier. It has multiple domains which are accessible with comfortable well-documented API. In this article, we used only the Runtime domain but CDP has way more. One of them is the Network domain which exposes information about HTTP, file, data, and other requests and responses, their headers, bodies, timing, etc.</p><p>I came around CDP when I was looking for a way to collect Memory usage of my local server in a specific format, Chrome built-in DevTool had nothing to offer for this, after some research I came to CDP and the idea of having my own Debugger+Profiler.</p><p>I hope this was interesting.</p><p>Full code available <a href="https://github.com/dudupopkhadze/override-live-nodejs-server">here</a>, if you have any questions → <a href="https://twitter.com/dudupopkhadze">Twitter</a></p><p>Happy Coding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=445cee1f02b7" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/nodejs-modify-running-server-behaviour-from-outside-445cee1f02b7">NodeJS: Modify Running Server Behaviour From Outside</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dependency Injection in TypeScript]]></title>
            <link>https://levelup.gitconnected.com/dependency-injection-in-typescript-5fd1f6207f2?source=rss-e1e334c6848f------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fd1f6207f2</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Dudu Popkhadze]]></dc:creator>
            <pubDate>Mon, 08 Nov 2021 08:22:41 GMT</pubDate>
            <atom:updated>2021-11-11T08:23:34.440Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RbmfNyhuBb8G3LWh.png" /></figure><h3>Motivation</h3><p>For the past few years, Node became one of the most popular back-end solutions. It’s very easy to bootstrap an app on Node and start handling HTTP requests on the fly. But there is a problem, in most cases, Node apps get very complicated and coupled while they grow, so keeping domain and persistence layers separate becomes extremely hard.</p><p>To resolve this problem, we should dive into the world of <a href="https://www.digitalocean.com/community/conceptual_articles/s-o-l-i-d-the-first-five-principles-of-object-oriented-design">SOLID</a> architecture (one part of it for today). To write code according to <a href="https://www.digitalocean.com/community/conceptual_articles/s-o-l-i-d-the-first-five-principles-of-object-oriented-design">SOLID</a> guidelines, we need <a href="https://www.martinfowler.com/articles/injection.html">Dependency Injection</a>. It allows us to abstract the creation of classes to be separate from their implementation, which separates domain/business logic from all other layers and makes code easier to maintain.</p><p>There is no out of box solution for dependency injection in Node but we can implement our own DI service with help of TypeScript decorators.</p><p>Let’s dive in.</p><h3>Implementation</h3><p>Since decorators are still in experimental mode, we will need to enable them in <strong><em>tsconfig.json</em></strong></p><pre>{</pre><pre>  &quot;compilerOptions&quot;: {</pre><pre>    &quot;target&quot;: &quot;ES5&quot;,</pre><pre>    &quot;experimentalDecorators&quot;: true,</pre><pre>    &quot;emitDecoratorMetadata&quot;: true,</pre><pre>    &quot;outDir&quot;: &quot;dist&quot;</pre><pre>  }</pre><pre>}</pre><p>Let’s suppose we have a back-end application that is in desperate need of Logger. Logger can have many implementations, some can log messages directly in the console, some can store them in a database and others can be doing both at the same time.</p><p>Let’s define generic Logger abstract class which will be implemented by actual Loggers</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b6e871aa39a386f3202597c346e3530/href">https://medium.com/media/5b6e871aa39a386f3202597c346e3530/href</a></iframe><p>Nothing fancy here, just an abstract class with single log method. Now let’s take a look at possible Logger implementations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9fad2d70df0e7d8b0581c6e04aa9f1c/href">https://medium.com/media/a9fad2d70df0e7d8b0581c6e04aa9f1c/href</a></iframe><p>For each dependency, we will need a unique string identifier that will be mapped to the actual class instance</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9dd08726f782f1e49d76313dc8d6b93/href">https://medium.com/media/c9dd08726f782f1e49d76313dc8d6b93/href</a></iframe><p>Now we need some kind of manager class, which will store all initialised classes and inject them whenever dependant classes will require them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c91b5d2d20cb044de646e75694a6bcb/href">https://medium.com/media/9c91b5d2d20cb044de646e75694a6bcb/href</a></iframe><p>We have Logger classes we want to inject and DependencyManager which stores all needed mappings. The only thing that remains is <strong><em>@Inject </em></strong>decorator (read more <a href="https://www.typescriptlang.org/docs/handbook/decorators.html">here</a> about TypeScript decorators), which will take care of injecting actual instances of classes with help of our Dependency Manager</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/121510d38b1be893e8bc03a3dd288634/href">https://medium.com/media/121510d38b1be893e8bc03a3dd288634/href</a></iframe><p>At this point we are ready to use our inject decorator and see our code in action</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be2f1b9b61447520ee2ec7aa155dfb3e/href">https://medium.com/media/be2f1b9b61447520ee2ec7aa155dfb3e/href</a></iframe><pre>Output:<br>LogServiceA -&gt; test<br>LogServiceB -&gt; test</pre><p>We can also define injectable classes with easily with @Injectable decorator, since we already implemented @Injectable we just need to add a call with a unique id on top of the class declaration</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3df0f9456b574d4b16ad6162211b60d4/href">https://medium.com/media/3df0f9456b574d4b16ad6162211b60d4/href</a></iframe><pre>Output:<br>LogServiceC -&gt; test</pre><p>As you can see, Injectable also works as intedended.</p><h3>Summary</h3><p>This was a small intro to Dependency Injection in Node. Dependency injection does go much deeper than this, such as injecting directly into the constructor and using symbols instead of tokens (similar to how Angular does it). However, I hope this has given you a small insight into how Dependency Injection works behind the scenes. When I first started coding Dependency Injection was a black box that I knew was very helpful but had no idea how it worked, I hope after this article there is one more magical part about software engineering that you’re no longer in the dark about.</p><p>Code available <a href="https://github.com/dudupopkhadze/ts-dependency-injection">here</a>.</p><h4>Happy Hacking!</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fd1f6207f2" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/dependency-injection-in-typescript-5fd1f6207f2">Dependency Injection in TypeScript</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reverse Engineer React Hooks]]></title>
            <link>https://levelup.gitconnected.com/reverse-engineer-react-hooks-4abfb4163e37?source=rss-e1e334c6848f------2</link>
            <guid isPermaLink="false">https://medium.com/p/4abfb4163e37</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Dudu Popkhadze]]></dc:creator>
            <pubDate>Tue, 13 Oct 2020 10:01:04 GMT</pubDate>
            <atom:updated>2020-10-23T12:11:15.653Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UMkNVuenBfTN7KO6fzMpgA.jpeg" /></figure><p>It has been more than a year since I started using functional components. With functional components, we can use cool React hooks like useState, useEffect… etc. Hooks became one of the main building blocks of our application, and understanding how they work is key to becoming a better React developer. I personally think that one of the best ways of figuring out how does something work is trying to reverse engineer it.</p><h3>How can we do it?</h3><p>One of the coolest things about React hooks is that we can implement them with help of other hooks. So we can reverse engineer hooks while still using them, this will help us to better understand hooks logic and develop a proper mental model, without further ado, let’s implement some of the builtin hooks.</p><p><strong>Note 1: Real implementations use a totally different approach.</strong></p><p><strong>Note 2: I can not guarantee that our custom hooks will work as intended with the new concurrent mode.</strong></p><h3>useRef</h3><p>Let’s start with useRef because we are gonna use this hook as the main building block for our other implementations.</p><p>According to React <a href="https://reactjs.org/docs/hooks-reference.html">docs</a>:</p><blockquote>useRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for the full lifetime of the component.</blockquote><p>So we need some kind of “box” to store our value inside, what are some basic principles our custom <strong>useRef</strong> should follow?</p><ul><li>The box should contain a single object with one key = “current”</li><li>Box’s current should be mutable</li><li>Changin box’s current value should not trigger rerender</li></ul><p>Whenever I have to store some kind of object/data/value the first thing that comes to mind is of course State, can we implement useRef with the state? Let’s see</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6409e74ad7ccb4d422715d12502b40a/href">https://medium.com/media/f6409e74ad7ccb4d422715d12502b40a/href</a></iframe><p>Yes, I know that we should not use useState in ordinary functions and modify it directly,( btw you can check out <a href="https://medium.com/@ryardley/react-hooks-not-magic-just-arrays-cd4f1857236e"><strong>this article</strong></a> to better understand the reason behind that hook’s rule), but let’s make an exception this one time. You can check out <a href="https://codesandbox.io/s/my-custom-ref-vsg9z?file=/src/my-own-ref.ts"><strong>Live Demo</strong></a>, as we can see our custom hook behaves exactly as React’s builtin <strong>useRef</strong>. If you do not understand why does this work, go play around with the given demo, add some console.log-s in the <strong>useRenderCount</strong> hook and take a closer look at object stored in our “box”.</p><h3>useState</h3><p>What do we use <strong>useState</strong> for? If you are reading this article you already know what state is, so let’s write down some basic principles our custom <strong>useState</strong> should follow</p><ul><li>Should return a stateful value and a function to update it.</li><li>During the initial render, return state should be the same as initialState</li><li>During subsequent re-renders, the returned state should always be the most recent state.</li><li>The initialState argument can also be a function instead, which will be executed only on the initial render.</li><li>setState should have support for passing a function as an argument, which will receive the previous value, and return an updated value.</li><li>setState should not change on re-render.</li><li>Changing state should trigger rerender if newValue !== prevValue</li></ul><p>About the last item of our list, triggering rerender without React’s original useState hook is a pretty challenging thing to do(to be honest I do not think it’s possible without using useReducer), so we are gonna use a custom little hack hook, which will trigger rerender manually.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bcb4c82fa6a0a5a61835d78bf52a86d/href">https://medium.com/media/7bcb4c82fa6a0a5a61835d78bf52a86d/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18984df654697bac431f45ed2aefaa88/href">https://medium.com/media/18984df654697bac431f45ed2aefaa88/href</a></iframe><p>This time we are not breaking any React hooks rules and our hook is behaving exactly like builtin <strong>useState</strong>. <a href="https://codesandbox.io/s/my-custom-state-kfvyr">Live Demo</a>.</p><p>Some notable moments:</p><ul><li>We have to store information about whether it is <strong>first render</strong> or not</li><li>If it is first render, we should first initialize our state. <strong>If</strong> the passed argument is function -&gt; call that function -&gt; set the returned value as the current state. <strong>Else</strong> we just set our state to the given default value.</li><li>React does not like unnecessary re-renders, so to avoid that we should first take a snapshot of our current state, then get a new state from the given argument and force re-render only if prevState !== curState</li><li>Congrats now we have our own <strong>useMyState!</strong></li></ul><p><strong>Note: If you think that sometimes your React app re-renders without having reason to, you may find </strong><a href="https://reactjs.org/docs/strict-mode.html"><strong>React Strict Mode</strong></a><strong> interesting, it’s a pretty useful thing.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*u2aSCN3uCrX294jNPVadJw.png" /></figure><p>We implemented two hooks <strong>useMyRef</strong> and <strong>useMyState</strong>, If you ask me It’s pretty cool that our useRef implementation relies on useState and useState implementation - useRef.</p><p>But there are many other hooks that we use every day, currently, memorization and optimization is a very hot topic in the React community, so can we build <strong>useCallback</strong> and <strong>useMemo</strong> hooks? can we find some interchangeable connection between them like we did with ref and sate? Let’s see</p><h3>useCallback</h3><p>What do we use useCallback for?</p><p>That&#39;s pretty straightforward, useCallback returns the memoized version of callback which only changes if one of the dependencies has changed. Hm, memoizing something in React, that’s something <strong>useMemo</strong> does best.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed2ec11c8211559ff8f59578c9e3cf3f/href">https://medium.com/media/ed2ec11c8211559ff8f59578c9e3cf3f/href</a></iframe><p>Yeah, works like a charm, checkout <a href="https://codesandbox.io/s/use-my-callback-h2ins?file=/src/App.tsx"><strong>Live Demo</strong></a><strong>. </strong>But there is a small warning in React docs we should consider</p><blockquote><strong>You may rely on </strong><strong>useMemo as a performance optimization, not as a semantic guarantee.</strong> In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.</blockquote><p>So we need new <strong>useMyCallback</strong> which won’t rely on <strong>useMemo </strong>and<strong> </strong>will be the same<strong> </strong>every render if the dependencies array has not changed. <strong>Bonus Feature:</strong> maybe we are too lazy to pass deps array, let’s make our hook which won’t have a dependencies list and will be as efficient as the original one. Storing the mutable value in React and updating it whenever some conditions are met, we had this problem already earlier, the answer is the same - <strong>useRef</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9596d251380c3f5102fa03e23e2cbba/href">https://medium.com/media/d9596d251380c3f5102fa03e23e2cbba/href</a></iframe><p>Check out <a href="https://codesandbox.io/s/use-my-custom-callback-02t8z?file=/src/useMyBetterCallback.ts"><strong>Live Demo</strong></a>, if you do not understand why our hook works like this, the best way to get a good grasp at it would be to go back and check out our earlier implementations, then play around with the demo. Go ahead change our hook’s return value to fn directly, you will notice how render count will increase. Also, you can modify our hook like this - export const useMyBetterCallback = useCallback; . You will notice that it behaves just like our custom hook.</p><h3><strong>useMemo</strong></h3><p>If useCallback stores memoized function useMemo stores memoized value which is computed with the function we pass as an argument. Dependencies array logic is the same here, the difference in current and older arrays means that we should recompute value.</p><blockquote>Remember that the function passed to useMemo runs during rendering. Don’t do anything there that you wouldn’t normally do while rendering. For example, side effects belong in useEffect, not useMemo.</blockquote><p>Again we should store some kind of value which must be the same on every render until the dependencies array changes, which means we have to address our good old useRef again.</p><p><strong>Note:</strong> for useMemo hook implementation we will need some kind of equality function which will determine if previous and new dependencies lists are the same.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7167ab0968bc2c787dc87514bb8ea44/href">https://medium.com/media/a7167ab0968bc2c787dc87514bb8ea44/href</a></iframe><p>Checkout <strong>useMyMemo</strong> in action with this Live Demo. It does not matter for us how is areDeepEqual function implemented, we just know that it’s pure and works as expected.</p><p>We just need to store the previous dependencies list with useRef, then we compare it with the new list and execute the given function only if there is some difference between lists. Now our user will always have value computed with the newest function.</p><h3>Summary</h3><p>Functional Components are very effective but we need a different mental model when using them, sometimes it can be a little bit tricky, having solid knowledge of hooks saves us tons of time while debugging our code. As I mentioned above, trying to reverse engineer stuff can be very helpful while learning something new, this helped me a lot when I was starting with React hooks and encountered some bugs which took hours to find.</p><h4>I hope this was Interesting.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4abfb4163e37" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/reverse-engineer-react-hooks-4abfb4163e37">Reverse Engineer React Hooks</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Hooks  როგორ მუშაობს useEffect ?]]></title>
            <link>https://medium.com/@dudupopkhadze/react-hooks-%E1%83%A0%E1%83%9D%E1%83%92%E1%83%9D%E1%83%A0-%E1%83%9B%E1%83%A3%E1%83%A8%E1%83%90%E1%83%9D%E1%83%91%E1%83%A1-useeffect-b413afcb257c?source=rss-e1e334c6848f------2</link>
            <guid isPermaLink="false">https://medium.com/p/b413afcb257c</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Dudu Popkhadze]]></dc:creator>
            <pubDate>Mon, 05 Oct 2020 08:17:22 GMT</pubDate>
            <atom:updated>2020-10-05T12:06:46.406Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Hooks როგორ მუშაობს useEffect ?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eP-UliNrRWQ0sfpy.png" /></figure><p>ალბათ, ყველას, ვინც რეაქთს ვიყენებთ, ერთხელ მაინც გამოგვიყენებია useState ჰუკი რაიმე ქაუნთერის მნიშვენლობის შესანახად. რეაქთის ჰუკები უმეტეს შემთხვევაში საქმეს გვიადვილებენ და კომპონენტიდან კომპლექსური ლოგიკების ამოღების საშუალებას გვაძლევენ. მათი გამოყენება ერთი შეხედვით ადვილია, მაგრამ კარგად თუ არ გავიაზრებთ მათი მუშაობის მოდელს, შეიძლება, ბევრ პრობლემას წავაწყდეთ. რეაქთის კლას კომპონენტებიდან ფუნქციონალურ კომპონენტებაზე გადასვლა საკმაოდ რთული პროცესია, რადგან რეაქთი under the hood ამ ორს სხვადასხვანაირად ამუშავებს. ჩემი აზრით, ყველაზე “პრობლემურია” useEffect ჰუკი, რადგან ცოტა განსხვავებული მენტალური მოდელი გვჭირდება მასთან სამუშაოდ.</p><p>useEffect მუშაობის მოდელის გასაგეაბად საჭიროა კარგად გავიაზროთ რეაქთის ფუნციონალური კომპონენტები. ჩემი აზრით, პირველი, რაც კარგად უნდა გავიაზროთ არის ის, რომ თითოეულ რენდერს აქვს საკუთარი props და state. რას ნიშნავს ეს? ნებიმისმიერი სთეითის ან ჩამოწოდებული პარამეტრების ცვლილების შემთხვევაში რეაქთი თავიდან დაარენდერებს ჩვენს ელემენტს, უკვე განახლებული მნიშვნელობებით. ამის დემონსტრირებისთვის <strong>setTimeout </strong>დაგვეხმარება.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ae1coG-IzouPiJsNJTa5_Q.png" /></figure><p>რა მოხდება თუ ჩვენს ღილაკს მხოლოდ ერთხელ დავაკლიკებთ? რეაქთის კლასების შემდეგ შეიძლება, ვიფიქროთ, რომ 2.5 წამის შემდეგ ალერტში უკვე Counter currently at: 1 დაგვხდება, მაგრამ ფუნქციონალურ კომპონენტებში როგორც აღვნიშნეთ საქმე სხვაგვარადაა</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Flm3-c0xkX4fWxuy2Mfqxw.png" /></figure><p>რატომ მოხდა ეს? 2.5 წამის შემდეგ counter-ის მნიშვნელობა უკვე ერთია(ამას ეკრანზეც ვხედავთ), მაგრამ ალერტში მაინც 1 დაგვხვდა. როგორც უკვე ვთქვით, ყოველ რენდერზე ფუნქციონალურ კომპონენტს აქვს თავისი საკუთარი props და state , აქედან გამომდინარე, ერთი რენდერის განმავლობაში ჩვენი counter კონსტანტია, ალერტმა “დააფიქსირა” ქაუნთერის კონკრეტული მნიშვნელობა და 2.5 წამის შემდეგ გამოგვიტანა.</p><p>შეგვიძლია, ვთქვათ, რომ თუ ჩვენ კომპონენტში გვაქვს რაიმე ფუნქცია, რომელიც დამოკიდებულია/იყენებს props ან state-ს მაშინ ის ყოველი რენდერისთვის განსხვავებული/ინდივიდუალური იქნება.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VJspezX90uzROlAhSkaQrw.png" /></figure><p>პირველ რენდერზე ჩვენს კომპონენტს მსგავსი სტრუქტურა ექნება, counter შეგვიძლია წარმოვიდგინოთ როგორც კონსტანტა და ჩვენი აღწერილი ფუნციებიც ამ კონკრეტულ მნიშვნელობებს “დააფიქსირებენ”.</p><p>აღსანიშნავია, რომ ეს ასე არ ხდება რეაქთის კლასებში, იგივე სტრუქტურით კლასი ალერტში 1 გამოგვიტანდა, რადგან კლასების this ცვალებადია.</p><p>შევთანხმდით, რომ ფუნქციონალური კომპონენტი უცლვლელია (immutable), ეს დაგვეხმარება useEffect თან მუშაობის სწორი მენტალური მოდელის ჩამოყალიბებაში.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mShW0yS59Ulq_N7F2oIa_A.png" /></figure><p>რა მოხდება თუ ღილაკს ხუთჯერ დავაკლიკებთ?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/969/1*j1VipKOWxnVfE5UatDQOKw.png" /></figure><p>როგორც ვხედავთ, ეფეტებზეც ზუსტად იგივე ლოგიკა მოქმედებს რაც სხვა ფუნციების შემთხვევებში, ყოველ რენდერზე ჩვენი ეფექტი “დააფიქსირებს” ქაუნთერის კონკრეტულ მნიშვლეობას. რა მოხდება თუ console.log(counter)-ს setTimeout ფუნქციში ჩავსვამთ? რა თქმა უნდა, შედეგი იგივე იქნება. შეგვიძლია, წარმოვიდგინოთ, რომ ეფექტიც კონკრეტული რენდერის ერთგვარი ნაწილია.</p><p><strong>useEffect </strong>ჰუკის ეფექტურად გამოყენებისთვის საჭიროა, ვიცოდეთ როდის იძახებს რეაქთი ჩვენს ეფექტს.</p><ul><li>რეაქთი კონკრეტული props და state-ის მნივნელობებს “ჩამოგვაწოდებს” ჩვენს კომპონენტში</li><li>ჩვენი კომპონეტი დააბრუნებს შესაბამის ეფექტს(ებს) და შესაბამის UI-ს</li><li>შემდეგ რეაქთი კონკრეტულ რენდერერს(ჩვენს შემთხვევაში ReactDOM-ს) გადასცემს ჩვენს UI-ს.</li><li>ReactDOM ბრაუზერში გამოაჩენს ჩვენლს ელემენტს</li><li>და ბოლოს რეაქთი გამოიძახებს ჩვენს ეფექტში მოთავსებულ ფუნქციას.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wT_gq-RbU6l22k64RRF4vQ.png" /></figure><p>useEffect ჰუკში შეგვიძლია დავაბრუნოთ ფუნქცია, რომელიც კომპონენტის კონკრეტული რენდერის unmout-ის შემდეგ გამოიძახება. ამ კონკრეტულ შემთხვევაში ეფექტის საშუალებით ვლოგავთ ქაუნთერის მნიშვნელობას, თუ 1 წამზე მეტია რაც არ შეცვლილა, წინააღმდეგ შემთხვევაში ვაუქმებთ timeout-ს. ინტუიტიურად შეიძლება, ვიფიქროთ, რომ ეს ფუნქცია კომპონენტის გადარენდერებამდე გამოიძახება, მაგრამ რეაქთი ოპტიმიზაციის გამო(რერენდერი რომ უფრო სწრაფად მოხდეს) ამას ოდნავ მოგვიანებით აკეთებს. ახალი Flow ასეთი იქნება</p><ul><li>რეაქთი ჩამოგვაწოდებს განახლებულ props ან states (ან ორივეს)</li><li>ReactDOM განაახლებს/გადაარენდერებს ჩვენს კომპონენტეს</li><li>რეაქთი გამოიძახებს “დასუფთავების” ფუნქციას</li><li>რეაქთი გამოიძახებს ახალ ეფექტს</li></ul><p>როდის იძახებს რეაქთი ჩვენს ეფექტს უკვე ვიცით, მაგრამ არის შემთხვევები, როცა არ გვინდა ყოველ რენდერზე ჩვენი ეფექტის გამოძახება. მაგალითად, ეფექტის საშუალებით მოგვავქს ინფორმაცია სერვერიდან, ამის გაკეთება ყოველ რენდერზე არ არის აუცილებელი(კონკრეტული შემთხვევების გარდა). ზოგადად, რეაქთი under the hood ცდლილობს მაქსიმალურად შეამციროს აფდეითების რაოდენობა. კომპონენტების შემთხვევაში ის props ებს პირდაპირ ადარებს და მხოლოდ განსხვავებული პარამეტრების მიხედვით ააფდეითებს DOM-ს.</p><p>ფუნქციების შემთხვევაში სხვა სიტუაცია გავქვს, კონკრეტული ეფექტის გამოძახება გამოიწვევს თუ არა აფდეითს ამას რეაქთი მოცემული ფუნცქიის გამოძახების გარეშე ვერ დაადგენს, ყოველ რენდერზე ფუნქციის გამოძახება იმის დასადგენად არის თუ არა აფდეითი საჭირო არა ოპტიმალური იქნებოდა. ამ პრობელმის გადაჭრას რეაქთი <strong>deps </strong>მასივის საშუალებით გვთავაზობს. ელემენტებში ვსვამთ იმ ელემენტებს, რომლებზეც დამოკიდებულია ეფექტში ჩასმული ფუნქცია. რენდერის დასრულების შედმეგ რეაქთი შეადარებს ძველ და ახალ მასივს, თუ ერთი ელემენტი მაინც იქნება განსხვავებული მხოლოდ ამ შემთხვევაში გამოიძახებს ჩვენ ფუნქციას. ამის გააზრების შემდეგ stackoverflow-ზე useEffect ჰუკთან დაკავშირებით დასმულ ყველაზე ხშირ შეკითხვებზე პასუხებსადვილად გავცემთ:</p><ul><li><strong>useEffect</strong> რომელიც ყველა რენდერზე გამოძახებს ჩვენს ფუნქციას</li></ul><pre>useEffect(() =&gt; {<br>   //<br>}) //<strong> მასივის გარეშე, გაეშვება ყველა რენდერზე</strong></pre><ul><li><strong>useEffect</strong> რომელიც მხოლოდ პირველ რენდერზე გამოიძახებს ფუნქციას</li></ul><pre>useEffect(() =&gt; {<br>   //<br>},[]) <br>// <strong>ცარიელი მასივი</strong> -&gt; <strong>რეაქთი მხოლოდ ერთხელ(პირველ რენდერზე) გამოიძახებს ფუნქციას</strong></pre><p>გადავაკეთოთ ჩვენი პატარა კომპონენტი ისე, რომ ქაუნთერი ყოველ 1 წამში ერთით იზრდებოდეს.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*muAKYtORES-LynlkhLDgqw.png" /></figure><p>ერთი შეხედვით ყველაფერი მუშაობს, ქაუნთერიც იზრდება და ბარაუზერშიც განახლებულ მნიშვნელობას ვხედავთ.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7nl4rgfnGlifnzmTx5S-Rg.png" /></figure><p>ჩვენს ეფექტს ასე თუ გადავაკეთებთ და კონსოლს გავხსნით, დავინახავთ, რომ ინტერვალი ყოველ რენდერზე თავიდან <strong>იქმნება</strong> =&gt;<strong> ზრდის ქაუნთერს </strong>=&gt; <strong>ნადგურდება</strong>. თავის საქმეს აკეთებეს, მაგრამ როგორ შეიძლება ისე გავაკეთოთ, რომ ინტერვალი მხოლოდ ერთხელ შეიქმნას?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oXRkyFyEj4ogF1hKHc0aFw.png" /></figure><p>როგორც ვთქვით, თუ გვინდა, რომ ჩვენი ეფექტი ერთხელ გამოიძახოს რეაქთმა, ცარიელი მასივი უნდა გადავცეთ. მართლაც, ეფექტი ერთხელ გამოიძახება(ეს პრობლემა მოგვარდა), მაგრამ აქ ჩნდება მეორე პრობლემა, რომელიც სტატიის დასაწყისში განვიხილეთ. როდესაც ინტერვალი იქმნება ქაუნთერის მნიშვნელობა არის 0, ინტერვალი ამას იმახსოვრებს და ყოველ ერთ წამში <strong>setCounter(0+1) </strong>ფუნქციას იძახებს, რომელიც პირველი გამოძახების შემდეგ არანაირ ზეგავლენას არ ახდეს სთეითზე.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LSe8ySOgChw2kpLXW0gRHg.png" /></figure><p>მაშინ counter -ის deps მასივში ჩასმის შემდეგ ყველაფერი უნდა მოგვარდეს, არა? not so fast, ჩვენი კომპონენტი კვლავ ამუშავდა, მაგრამ პირველი პრობლემა, რისი გადაჭრაც გვინდოდა, კვლავ დაბრუნდა. ქაუნთერს ყოველ რენდერზე სხვადასხვა მნიშვნელობა აქვს, მეორე რენდერის შემდეგ რეაქთი ნახავს, რომ <strong>[0] ===[1] </strong>არ არის ჭეშმარიტი და თავიდან შექმნის ახალ ინტერვალს.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6mztWEdW0rVf4KHhMTZ9Ag.png" /></figure><p>ყოველ რენდერზე ახალი ინტერვალის თავიდან ასცილებლად დაგვეხმარება useState ჰუკის cheat mode ანუ <strong>Functional updater</strong>. იდეაში, ქაუნთერი ჩვენს ეფექტში გვიჭირდებოდა მხოლოდ ახალი ქაუნთერის მნიშვნელობის გამოთვლისთვის, ამის გაკეთება პირდაპირ setCounter ით შეგვიძლია. რეაქთსაც არ ვატყუებთ ჩვენი Dependencies შესახებ(ქაუნთერს არსად არ ვიყენებთ ეფექტში) და ჩვენი კომპონენტიც მუშაობს.</p><h3>რატომ დავწერე ეს სტატია?</h3><p>თავიდან ვფიქრობდი, რომ ჰუკებით დეველოპმენტის პროცესი გაცილებით ადვილი იქნებოდა, მაგრამ პრაქტიკამ მაჩვენა, რომ შესაბამისი მენტალური მოდელის გარეშე ჰუკების გამოყენება საკმაოდ სახიფათოა. დიდი დრო დამიხარჯავს <strong>useEffect</strong> -ში არასწორი დეფენდესების ჩაწოდებით გამოწვეული ბაგების ძიებაში:) ეს გრძელდებოდა მანამ, სანამ კარგად არ გავიაზრე ეფექტების მუშაობის პრინციპი, იმედია, ეს სტატია ვინმეს ცოტა დროს დაუზოგავს.</p><p><strong>useEffect</strong> -ის საშუალებით სერვერთან ურთიერთობის საუკეთესო პრაქტიკები</p><ul><li><a href="https://www.robinwieruch.de/react-hooks-fetch-data">How to fetch data with React Hooks? - RWieruch</a></li><li><a href="https://medium.com/@dudupopkhadze/%E1%83%A5%E1%83%90%E1%83%A0%E1%83%97%E1%83%A3%E1%83%9A%E1%83%98-react-renderer-4866cf8e94e">ქართული React Renderer</a></li></ul><p>ბლოგის ინპსირაცია : <a href="https://overreacted.io/">https://overreacted.io/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b413afcb257c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ქართული React Renderer]]></title>
            <link>https://medium.com/@dudupopkhadze/%E1%83%A5%E1%83%90%E1%83%A0%E1%83%97%E1%83%A3%E1%83%9A%E1%83%98-react-renderer-4866cf8e94e?source=rss-e1e334c6848f------2</link>
            <guid isPermaLink="false">https://medium.com/p/4866cf8e94e</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Dudu Popkhadze]]></dc:creator>
            <pubDate>Thu, 24 Sep 2020 11:26:05 GMT</pubDate>
            <atom:updated>2020-09-24T21:44:19.025Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/789/1*t4ZmTC3vB6o-IhcHrCUMoA.jpeg" /></figure><h4>რატომ ვწერ ამ სტატიას?</h4><p>ამ სტატიის საშუალებით ვეცდები რაიმე ახალი და სასარგებლო ინფორმაცია მოგაწოდოთ React-ის შესახებ და ამავდროულად, რა თქმა უნდა, ჩემი საკუთარი ცოდნაც გავიღრმავო. სტატია განკუთვნილია მათთვის, ვინც იცის ზოგადად რა არის რეაქთი, რაში ვიყენებთ მას და რას ნიშნავს “კომპონენტის დარენდერება”.</p><h4>რისგან შედგება რეაქთი?</h4><p>როგორც ვიცით, რეაქთი არის ბიბლიოთეკა, რომელიც ფრონტ-ენდ დეველოპმენტის პროცესში გვეხმარება, მაგრამ როგორ აკეთებს ის ამას?</p><p>რეაქთი შეგვიძლია დავყოთ სამ ფუნდამენუტრ ნაწილად:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aall6XWcci017m76kbo4Zw.png" /></figure><ul><li><strong>React API</strong> ნაწილი, რომელთანაც ჩვენ ვურთიერთობთ, ვქმნით კომპონენტებს და ასე შემდეგ.</li><li><strong>React Reconciler </strong>ეს არის ფექიჯი, რომელიც აანალიზებს ჩვენს აპლიკაციაში მომხდარ ცვლილებებს O(n) დროში, სადაც n ჩვენს ხეში ელემენტების რაოდენობაა. ამის შემდეგ Reconciler გამოიძახებს რენდერერის შესაბამის მეთოდებს.</li><li><strong>React Renderer </strong>რენდერერის მთავარი ფუნქციაა ჰოსთის API-სთან მუშაობა. როცა Reconciler გამოთვლებს დაასრულებს, ის გამოიძახებს რენდერის შესაბამის ფუნციებს და უკვე რენდერერია პასუხისმგებელი მომხმარებელთან ცვლილებების ასახვაზე. სწორედ ამ მაღალი დონის აბსტრაქციის გამოა შესაძლებელი რეაქთის სსხვადასხვა პლატფრომაზე გამოყენება. ყველაზე ფართოდ ცნობილია, რა თქმა უნდა, ReactDOM და React Native, მაგრამ კიდევ ბევრი <a href="https://github.com/chentsulin/awesome-react-renderer">საინტერესო რენდერ</a>ი არსებობს.</li></ul><p>React Reconciler-ს აქვს ორი რეჟიმი: მუტაციური და მუდმივი. მუდმივი რეჟიმი გამოიყენება მაშინ, როდესაც host გარემოში გვხვდება უცვლადი ელემენტების ხე. მაგალითად, თუ შევქმნით რამე &lt;Image/&gt; ელემენტს და გარკვეული დროის შემდეგ რეაქთს ვეტყვით რომ ამ ელემენტის სიმაღლე გაზარდოს, რეაქთი შექმნის ამ ელემენტის კლონს გაზრდილი სიმაღლთ, ძველ ელემენტს ჩაანაცვლებს ახლით და ამ ელემენტის მშობელს აცნობებს რომ მისი შვილი შეიცვალა. მოკლედ, უკვე დარენდერებული ელემენტი ცვლილებას ვეღარ განიცდის, მსგავსი მიდგომა გამოიყენება React-Native-ში და საკმაოდ ბევრი დადებითი მხარე აქვს. ჩვენ მუტაციური რეჟიმის საშუალებით შევქმნით ReactDOM-ის მინიმალისტურ კლონს.</p><pre>// შევქმნათ ახალი რეაქთის აპლიკაცი create-react-app-ის საშუალებით<br>npx create-react-app my-renderer<br>cd my-renderer <br>yarn add react-reconciler<br>yarn start</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f27faaa641c4c25c2544753ce9a4f972/href">https://medium.com/media/f27faaa641c4c25c2544753ce9a4f972/href</a></iframe><p>src ფოლდერში შევქმნათ ახალი ფაილი, რომლიდანაც დავაექსპორტებთ ჩვენს რენდერერს. რეალურად რენდერერს სამი პარამეტრი გადაეცემა, მესამეა ქოლბექ ფუნქცია, რომელიც გამოიძახება რენდერის დასრულების შემდეგ, მაგრამ ეგ ამ ეტაპზე არ გვაინტერესებს. რენდერესირ ჩანაცვლების შემდეგ, როგორც მოსალოდნელი იყო, <a href="http://localhost:3000/"><em>http://localhost:3000/</em></a><em> -</em>ზე არაფერი აღარ ჩანს და კონსოლიც შეცდომებითაა სავსე. სრულყოფილი რენდერერის შესაქმნელად ბევრი ფუნქციის იმპლემენტაციაა საჭირო, მაგრამ ჩვენი SuperRender-ისთვის რამდენიმეც საკმარისი იქნება.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c851f854b8adb74416a85e6f1a72ed0/href">https://medium.com/media/4c851f854b8adb74416a85e6f1a72ed0/href</a></iframe><p>შევქმნათ notRelevantConfig და hostConfig ობიექტები, რომლებშიც აღვწერთ Reconciler-ისთვის საჭირო ფუნქციების ჩვენებურ იმპლემენტაციას. notRelevantConfig-ში აღწერილი ფუნქციები სინამდვილეში ძალიან მნიშვნელოვანია, მაგრამ ჩვენი რენდერერისთვის სასიცოცხლოდ მნიშვნელოვანი ამ ეტაპზე არცერთი არ არის. როგორც ვხედავთ, სულ რაღაც 40 ხაზი (ნახევარიი უბრალოდ ფუნქციების დეკლარაციაა) კოდის დამატების შემდეგ ჩვენი აპლიკაცია კვლავ ამუშავდა.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qz3cA5w0Enghb1SwuXcqXA.png" /></figure><p><strong>createInstance</strong> ეს არის ფუნქცია, რომელსაც Reconciler იძახებს ახალი ელემენტის შესაქმნელად. რადგან ჩვენს შემთხვევაში host გარემო არის ვები, ვიყენებთ DOM API-ის ჩვენს ხეში ელემენტების შესაქმნელად . type არის ამ კონკრეტული ჰოსთში ჩაშენებულ ელემნტის ტიპი, ჩვენ შემთხვევაში ეს შეიძლება იყოს: “div”,”h1&quot;, “span”... სხვა ჰოსთ გარემოში რომ ვიყოთ შეიცვლებოდა სწორედ ეს ტიპები. მაგალითად, React-Native-ს რომ ვიყენდებეთ ტიპი შეიძლება ყოფილიყო: “Image”, ”View”…</p><p>წინა ნაწილში ვთქვით, რომ Reconciler უნივერსალური და სრულიად დამოუკიდებელია რენდერერისგან, ამიტომ ჩნდება კითხვა: რატომ აბრუნებს createInstance ფუნცია DOM-ის ელემენტს თუ Reconciler-მა არაფერი არ იცის DOM-ის შესახებ? Reconciler-ს ეს ელემენტი მხოლოდ ერთი მიზნით ჭირდება, ის ამ ელემენტს ისევ ჩვენ ჩამოგვაწოდებს, რათა საჭიროების შემთხვევაში DOM API-ის საშუალებით მოვახდინოთ მათზე მანიპულაცია, წავშალოთ ან ჩავამატოთ როგორც შვილობილი ელემენტი. აღსანიშნავია, რომ მარტო createInstance ფუნქციის იმპლემენტაციით აპლიკაცია ჩაირთვება, მაგრამ ეკრანზე არაფერი არ გამოჩნდება. ამის მიზეზია ის, რომ Reconciler ეცდება გამოიძახოს ჩვენი რენდერერის appendChild ფუნქცია, რომლის საშუალებითაც ხე უნდა აიწყოს. ამ ფუნქციის იმპლემენტაციის არ არსებობის გამო ჩვენს ხეში იქნება ერთი &lt;div id=”root”&gt; ელემენტი 0 შვილით.</p><pre>if (props.className) {  <br>      domElement.className = props.className;<br>}</pre><p>საინტერესოა ეს ადგილი createInstance ფუნქციაში, როგორც ვხედავთ ჩვენს მიერ ელემენტზე მინიჭებული სტილების გადაცემა პირდაპირ ხდება DOM-ის ელემენტზე. ანუ, უკვე ჩვენ ვწყვეტთ როგორ აისახება React API-ს საშუალებით ელემენტისთვის გადაცემული პარამეტრები DOM-ში. ეს საშუალებას გვაძლევს შევქმნათ ჩვენი საკუთარი ქართული რეაქთი, რომლის გამოყენებითაც კოდს ესე დავწერთ:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QpHK4XOCMFTsV9JMe_VRbQ.png" /></figure><p>ჩვენი ქართული რეაქთი შეგვიძლია შემდეგ საფეხურზე გადავიყავნოთ, გავაუმჯობესოთ ჩვენი რენდერერი ისე, რომ რეაქთის ლოგოზე კლიკის შემდეგ რეაქთის ლოგო ჩანაცვლდეს ყანწით და ყოველ 1 წამში შეიცვალოს მთლაინი აპლიკაციის ფონის ფერი. ჩვენმა ახლანდელი რენდერერმა ჯერჯერობით არ იცის ეს როოგორ გააკეთოს.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25699b976d4a7125a1c9821bdeb8d8ab/href">https://medium.com/media/25699b976d4a7125a1c9821bdeb8d8ab/href</a></iframe><p>კლიკზე ლოგოს შეცვლა ბევრი სხვადსხვა ვარიანტით შეგვეძლო გაგკვეთებინა .ძირითად ორი ვარიანტი ინქბოდა : 1. კლიკზე კონკრეტული &lt;img/&gt; თაგის წაშლა და საერთოდ ახალის დარენდერება სხვა სორს ფროფერთით. 2. კლიკზე უბრალოდ შევცვალოთ უკვე არსებული ელემენტის სორს ფროფერთი.</p><p>მე პირადად ავირჩიე მეორე ვარიანტი, ანუ ელემენტის წაშლა ჩვენი ხიდან არ მოხდება, ამის გამო, შეგვიძლია ჩვენი რენდერერის წაშლაზე პასუხისმგებელი ფუნქციების დაკომენტარება და აპლიკაცია მაინც იმუშავებს. ამის მთავარი მიზეზი ისა, რომ Reconciler ძალიან ჭკვიანია და ხვდება, რომ ელემენტი იგივეა და უბრალოდ მისი სორს ფროფერთი შეიცვალა, ამიტომ ახალ სორს ჩააწოდებს და ელემენტი იგივე დარჩება. აგრეთვე მოგვიწია createInstace ფუნქციის მოდიფიკაცია, რადგან წინა ვერსიაში ჩვენს რენდერერს წარმოდგენა არ ჰქონდა რომ onClick და “ფონი” პარამეტრები არსებობდნენ.</p><p>prepareUpdate &amp; commitUpdate ეს 2 ფუნქცია აგებს პასუხს ელემენტის ფროფერთის განახლებაზე, Reconciler ჯერ გამოიძახებს prepareUpdate-ს, რომლის საშუალებითაც ვადარებთ ძველ და ახალ პარამეტრებს, თუ ვიპოვით განსხვავებას ვაბრუნებთ ობიექტს, რომელიც შიეცავს მხოლოდ განახლებულ პარამეტრებს. reconciler შემდეგ prepareUpdate-ს დაბრუნებულ ობიექტს ჩამოგვაწოდებს commitUpdate-ში, სადაც უკვე ამ პარამეტრების მიხედვით მოვახდენთ DOM API-ს გამოყენებით შესაბამისი ელემენტის მუტაციას.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F0MGkQqox1OGQUqAw59%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F0MGkQqox1OGQUqAw59%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F0MGkQqox1OGQUqAw59%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="373" frameborder="0" scrolling="no"><a href="https://medium.com/media/94bddb4e7922960cb1a880c281e261f3/href">https://medium.com/media/94bddb4e7922960cb1a880c281e261f3/href</a></iframe><p>რადგან ჩვენს კონფიგ ობიექტში წაშლაზე პასუხისმგებელი ფუნქციები დაკომენტარებული გვაქვს, Reconciler ვერ შეძლებს რამე ძველი ელემენტის წაშლას და მის ნაცვლად სხვა ელემენტის ჩასმას. ამის დემონსტრირებისთვის ჩვენი App.js-ის ოდნავ შეცვალაც საკმარისია</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uAv-S_NOi7izOE25yH9tXQ.png" /></figure><p>თუ კოდს ესე გადავწერთ(არ გირჩევთ conditional-render-ის მსგავსი სტილით გამოყენებას :დ) ჩვენი რენდერერი უკვე ვერ შეძლებს ჩვენი ხიდან რეაქთის ლოგოს ამოშლას და კლიკის შემდეგ მივიღებთ ასეთ შედეგს</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*gwhKyxn2wKZ4Fm0ztffRqQ.png" /></figure><p>სად არის პრობლემა ? useState სწორად მუშაობს, მაგაზე Reconciler ზრუნავს და ჩვენ არ გვეხება, უბრალოდ ჯერ ჩვენმა რედერერმა არ იცის როგორ წაშალოს ელემენტი. logoWasClicked უკვე ჭეშმარიტია, მაგრამ ხეში ძველი ელემენტი მაინც დარჩა. ამის მოსაგვარებლად დაგვჭირდება წაშლაზე და უკვე არსებულ ხეში ჩამატებაზე პასუხისმგებელი ფუქნციების იმპლემენტაცია, რომლებიც ჩვენს კონფიგ ობიექტში უკვე გვაქვს და უბრალოდ დაკომენტარებულია. კომენტარების მოხსნის შემდეგ, ჩვენი რენდერერი შეძლებს უკვე არსებული ხიდან ელემენტის წაშლას, რის შედეგადაც აპლიკაცია კვლავ ამუშავდება.</p><h4>შეჯამება</h4><p>ამ სტატიით ვეცადე საინტერესოდ გადმომეცა თქვენთვის რა გზას გადის ჩვენს მიერ დაწერილი რეაქთის კოდი host გარემომდე და როგორ მუშაობს რეაქთ რენდერი.</p><p><a href="https://github.com/dudupopkhadze/georgian-react-renderer">სორს კოდი</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4866cf8e94e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>