<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ThankGod Ukachukwu</title>
    <description>The latest articles on DEV Community by ThankGod Ukachukwu (@tksilicon).</description>
    <link>https://dev.to/tksilicon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F349436%2Fb5a6c48d-5229-4381-b44d-a91255608126.jpeg</url>
      <title>DEV Community: ThankGod Ukachukwu</title>
      <link>https://dev.to/tksilicon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tksilicon"/>
    <language>en</language>
    <item>
      <title>Designing Google Calculator with React Redux Hooks</title>
      <dc:creator>ThankGod Ukachukwu</dc:creator>
      <pubDate>Thu, 23 Apr 2020 18:41:30 +0000</pubDate>
      <link>https://dev.to/tksilicon/designing-google-calculator-with-react-redux-hooks-2fom</link>
      <guid>https://dev.to/tksilicon/designing-google-calculator-with-react-redux-hooks-2fom</guid>
      <description>&lt;p&gt;Preamble&lt;/p&gt;

&lt;p&gt;Redux made complete sense after I watched the video tutorial by Dan Abramov — the creator of Redux — and the tutorial by Reinald Reynoso. See the reference list at the end of the article.&lt;br&gt;
Though I am late to the Redux party but I still think it is worthwhile writing this tutorial. Especially for those coming newly to React Redux that would find any relevant knowledge useful. This one could pop up in their frenzy search for knowledge.&lt;/p&gt;

&lt;p&gt;The Concept&lt;/p&gt;

&lt;p&gt;Redux “is a predictable state management library for frontend frameworks”. It’s popular in React. There are others like Mobx, Flux. But Redux’s simplicity and thinness makes it a library of choice in React.&lt;/p&gt;

&lt;p&gt;One thing they often say about Redux is - “finding the right use case”. So somehow you need to have an application where you need to save state.&lt;br&gt;
Don’t think you’d need some kind of complex application to use Redux. The simplest application where you need to save some state can suffice. I was thinking I have to get involved in a big project where I need to save user details, like school management or hospital management application or a social media app or an enterprise application before I would find that ‘use case” . Turned out that the calculator I designed to deepen my knowledge of React needed Redux because it could use some state saving.&lt;/p&gt;

&lt;p&gt;I attempted to design the calculator you find when you type “calculator” or “Google calculator” on Google. &lt;a href="https://simplegooglecalculator.herokuapp.com/"&gt;The picture below is the one I developed, click here to see it.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hlClhkMD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w2jrcf4nb3q93rvf0fqa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hlClhkMD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/w2jrcf4nb3q93rvf0fqa.png" alt="Simple Calulator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How it Works&lt;/p&gt;

&lt;p&gt;That calculator has “Ans” button to retrieve current answer, it saves the answer in memory, and also shows you the operands above the upper display. You can recall the last answer with Ans and use. When you do a computation, you replace the answer with the result or mathematical expression. If you press AC (all clear), you clear everything including the Answer saved. On start, Once you click a number (operand) or operator, the answer updates to 0 . If you do a calculation, the upper display shows you the expression, and display the answer in the lower display.&lt;/p&gt;

&lt;p&gt;War Games&lt;/p&gt;

&lt;p&gt;So here’s where Redux comes in. On start, as soon as we click a clickable operand/operator, the answer updates to 0. When we finish a calculation and click equal, we update answer and display the expression. If we click on Ans button. We will retrieve the answer and use in an expression. If we click a new operand or operator, we start a new computation but our previous answer is still saved. So tracking this state I think can be done with Redux, I don’t think it may be the most appropriate use case but that’s what I did. So we have three states - EQUAL, OPERATION and CLEAR.&lt;/p&gt;

&lt;p&gt;To use redux we have to install it. You should have created your app with npm create-react-app MY-APP and before then installed CRA (create-react-app) all with npm or yarn. And get the React binding.&lt;/p&gt;

&lt;p&gt;npm install redux ………………………………………………………………&lt;br&gt;
npm install react-redux ………………………………………………………&lt;/p&gt;

&lt;p&gt;Our actions and reducers are the main building blocks of using Redux. These are functions. Normal functions. Only that it’s recommended to use pure functions. A pure function returns a predictable result and do not have observable side effects. A pure function should not have side effects that causes our state to be unpredictable, such as accessing databases, running computations that do not bring the same outcome all the time. However, Redux Thunk solves the problem of asynchronous behaviour. See reading list at end for more. Also see the third video in Dan Abramov’s egghead video tutorial.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5LZb1fMT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cbe3ppzhxyiysv49tk6x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5LZb1fMT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cbe3ppzhxyiysv49tk6x.png" alt="Pure/Impure Functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take Action And Reduce The Tension&lt;/p&gt;

&lt;p&gt;So your actions are functions that ship plain objects to Redux store to be accessed to manage state. One of the object member should be a enumerable type. Such as STORE, OPERATION, CLEAR. Here are the action objects used in the Calculator. Each action describes if we are doing an operation, get the result of expression using equals operand or clearing the display.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lAYWRD_C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7s09mb5ig25x0h5kmbx3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lAYWRD_C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7s09mb5ig25x0h5kmbx3.png" alt="Actions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reducers are functions which are fed into the Redux store which uses a switch statement based on the action sent returns a desired state or the default state. Here's the reducer for this Calculator. Our actions are imported into the reducer. We insert the initialstate (depending on the logic of our application) and create the store. The createstore method can be fed the initialState too. It takes a third argument which can be a middleware like Redux Thunk. We can combine reducers and pass them into the createstore method if you have multiple reducers to track.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yBS_vHB4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qm9wwtmevlwm7logxz4z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yBS_vHB4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qm9wwtmevlwm7logxz4z.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we go to the component which needs redux and feed in the provider of the store to our component. For this use case, it’s the App.js where the calculator component lives.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zYSyyn9q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9bs6kwbac3psajcieok9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zYSyyn9q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9bs6kwbac3psajcieok9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now to the Calculator components. Find screenshots of relevant parts below. It uses functional components and hooks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UGeeGo_d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0k6c0l3bl09bo2n2mfd3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UGeeGo_d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0k6c0l3bl09bo2n2mfd3.png" alt="Calculator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We need the useDispatch and useSelector hooks for functional components. The useDispatch is to to send actions which delivers state objects to our store. The selector is used to access the state of the store. Here is where I used them below. I used this function to add the operand and operators and I need to track the state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7lGcyX2Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t9ocwxf79qke0fqrv4i9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7lGcyX2Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t9ocwxf79qke0fqrv4i9.png" alt="Dispatch"&gt;&lt;/a&gt;&lt;br&gt;
When computation is done. Answer to expression computed is dispatched. When AC, cleans up the display area too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ffeCUfTF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ngvaq192ckwfh5jm4xul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ffeCUfTF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ngvaq192ckwfh5jm4xul.png" alt="Dispatch"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So basically we are able manage the three state we need. Something tells me this can be done with ordinary hooks. In the bid to learn I have to make up a use case.&lt;/p&gt;

&lt;p&gt;Find below where I assessed the value from useSelector.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9cCbC-jS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6rn8uaz4p4asjhjrd1ql.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9cCbC-jS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6rn8uaz4p4asjhjrd1ql.png" alt="HTML"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;useSelector returns the state object. So don’t mistake it and put a [] around it. Like other hooks that return an array. Access it like a normal object. About preloaded state. On the Redux documentation, they discussed preloadedState in which the createStore method should be given a preloadedState to set initial state. Else it will be undefined. createStore takes three parameters, the reducer, preloadedState, and enhancer. PreloadedState State should be a plain object. Don’t pass in for instance “initialState” instead of initialState() which is one of your actions that returns a plain object. Then if you didn’t give it a preloadedState, the useSelector returns a DefaultRootState which is undefined.&lt;/p&gt;

&lt;p&gt;Redux may not be the silver bullet in state management even in React. Kent C Odds has done a good tutorial on using Context API. You can read the list at the end and also one written by Ebenezer Don who said we should discard React + Redux and use hooks + Context.&lt;/p&gt;

&lt;p&gt;Nonetheless. This is what Redux does, manages application state. This is just basic. Watch Dan Abrov videos for more.&lt;/p&gt;

&lt;p&gt;From the Experts:&lt;br&gt;
&lt;a href="https://egghead.io/courses/getting-started-with-redux"&gt;Dan Abramov’s videos, see video 3 for pure functions and the rest of it for basic and advanced Redux.!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://levelup.gitconnected.com/react-redux-hooks-useselector-and-usedispatch-f7d8c7f75cdd"&gt;Reinald Reynoso’s tutorial, one of the two that made it make sense.&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.taniarascia.com/redux-react-guide/"&gt;Tania Rascia is comprehensive guide, it includes middleware usage Redux thunk for asynchronous behavior.&lt;/a&gt;&lt;br&gt;
&lt;a href="https://kentcdodds.com/blog/application-state-management-with-react"&gt;Kent C Odds (another React guru) advised that we “use context only when prop drilling really becomes a problem”.!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://kentcdodds.com/blog/application-state-management-with-react"&gt;Ebenezer Don thinks we should all “Use Hooks + Context, not React + Redux”&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/javascript-in-plain-english/replacing-redux-with-custom-react-hooks-for-state-management-5d04592fd6d7"&gt;For replacing Redux with custom hooks, see Jolanta’s tutorial.&lt;/a&gt;&lt;br&gt;
Meanwhile the calculator is a work in progress.&lt;/p&gt;

&lt;h1&gt;
  
  
  Staysafe
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>Enabling ES6 Features (Import and Export Default) In Node.js with Babel</title>
      <dc:creator>ThankGod Ukachukwu</dc:creator>
      <pubDate>Tue, 14 Apr 2020 14:09:05 +0000</pubDate>
      <link>https://dev.to/tksilicon/enabling-es6-features-import-and-export-default-in-node-js-with-babel-34e</link>
      <guid>https://dev.to/tksilicon/enabling-es6-features-import-and-export-default-in-node-js-with-babel-34e</guid>
      <description>&lt;p&gt;Despite preponderance of tutorials on existing knowledge, it is important to document our learning experiences so that newbies can learn from different perspectives. Most important, some of the things we learned or issues encountered and later resolved are scattered all over the ecosystem in questions on Stackoverflow and Github and blogs and tutorials on medium and many blogs out there.&lt;/p&gt;

&lt;p&gt;To get past them we glean information from many sources. As a late comer to programming writing and tutorial, my goal is to try and bring these disparate knowledge I encounter together so that anyone who happens upon it will learn from all those sources at once and from an updated viewpoint. &lt;/p&gt;

&lt;p&gt;Sometimes, it is important to read the perspective of a learner so those who are learning can have a feel of what it was like before crossing the rubicon to the other side, from the unknown to the known.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/r/?url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F40294870%2Fmodule-exports-vs-export-default-in-node-js-and-es6"&gt;Node.js&lt;/a&gt; is the Javascript standard platform for server-side development. Express is the oldest and most popular web framework for Node.js. Most beginners start from learning express. &lt;/p&gt;

&lt;p&gt;The default standard for Node.js module handling is CommonJS. Where we have &lt;a href="https://blog.risingstack.com/node-js-at-scale-module-system-commonjs-require/"&gt;module.exports and require&lt;/a&gt;. For Node.js in ES6 environment we will need a compiler to translate ES6 to &lt;a href="https://blog.risingstack.com/node-js-at-scale-module-system-commonjs-require/"&gt;CommonJS&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Recent Node.js 13.2.+ supports &lt;a href="https://medium.com/r/?url=https%3A%2F%2Fhacks.mozilla.org%2F2018%2F03%2Fes-modules-a-cartoon-deep-dive%2F"&gt;ECMAScript modules&lt;/a&gt; inbuilt but the LTE (Latest Stable Edition) v12.16.2 does not. The latest support most likely will take some months or even another year before projects begin to move to that version features. The support for &lt;a href="https://dev.to/iggredible/what-the-heck-are-cjs-amd-umd-and-esm-ikm"&gt;Import and Export statements&lt;/a&gt; in the LTE needs to be enabled and run via - experimental-module to use them. Example:&lt;/p&gt;

&lt;p&gt;"start: node - experimental-modules ./src/index.js"&lt;/p&gt;

&lt;p&gt;The key to enabling this behaving is including the below in package.json&lt;br&gt;
&lt;a href="https://nodejs.org/api/esm.html"&gt;"type": "module"&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again, one of the &lt;a href="https://stackoverflow.com/questions/61146112/error-while-loading-config-you-appear-to-be-using-a-native-ecmascript-module-c"&gt;typical errors&lt;/a&gt; that will be encountered is:&lt;/p&gt;

&lt;p&gt;Error while loading config - You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously&lt;/p&gt;

&lt;p&gt;This is because your project would have generated or you may have added babel config as Javascript file extension(babel.config.js). &lt;a href="https://babeljs.io/"&gt;Babel&lt;/a&gt;, very popular, "is a JavaScript compiler." Simply as it is on the &lt;a href="https://github.com/babel/babel"&gt;github page&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;Babel is a tool that helps you write code in the latest version of JavaScript. When your supported environments don't support certain features natively, Babel will help you compile those features down to a supported version.&lt;/p&gt;

&lt;p&gt;So we get modern Javascript features with Babel compiler. So in our Node.js project, the use of .mjs, cjs or js extension for the babel.config.extension is one of the requirements. For v12. the right file extension is .cjs which means CommonJS. So the configuration at the root of my directory becomes babel.config.cjs. See more about it &lt;a href="https://babeljs.io/docs/en/config-files"&gt;here&lt;/a&gt; on babel docs. For deeper dive read &lt;a href="https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/"&gt;this&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Don't forget that for all these to work on Node.js, this configuration is necessary in the babel.config.cjs.&lt;/p&gt;

&lt;p&gt;module.exports = { &lt;br&gt;
  presets: [&lt;br&gt;
     [ &lt;br&gt;
       '@babel/preset-env',&lt;br&gt;
       {&lt;br&gt;
        targets: {&lt;br&gt;
          node: 'current'&lt;br&gt;
         }&lt;br&gt;
       }&lt;br&gt;
     ]&lt;br&gt;
   ]&lt;br&gt;
};&lt;br&gt;
Here is a related interesting read I will &lt;a href="https://humanwhocodes.com/blog/2019/01/stop-using-default-exports-javascript-module/"&gt;recommend&lt;/a&gt;. #Staysafe.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
