{"@attributes":{"version":"2.0"},"channel":{"title":"DEV Community: Reza Bozorgi","description":"The latest articles on DEV Community by Reza Bozorgi (@rezab).","link":"https:\/\/dev.to\/rezab","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%2F393102%2Fb38b6016-a929-4765-bd3e-bbd997dcba1c.jpg","title":"DEV Community: Reza Bozorgi","link":"https:\/\/dev.to\/rezab"},"language":"en","item":[{"title":"Common React Design patterns: Custom Hooks","pubDate":"Tue, 31 May 2022 19:22:07 +0000","link":"https:\/\/dev.to\/rezab\/common-react-design-patterns-custom-hooks-4i9p","guid":"https:\/\/dev.to\/rezab\/common-react-design-patterns-custom-hooks-4i9p","description":"<p>I started a series called \"Common React Design Patterns\" in which I want to practice some of the major design patterns used in React with each other. So we can be confident enough to apply it in our day-to-day development.<\/p>\n\n<p>Knowing these patterns is helpful because it can save our time as developers, and it would present the application as a high-performance and delightful experience for our customers.<\/p>\n\n<h2>\n  \n  \n  Custom Hooks\n<\/h2>\n\n<p>One of the basic design patterns that we use daily is \"Custom Hooks.\" It is vastly used as a preferable way to modularize and apply the DRY principle in our application. We can share complex behavior between multiple components by using a custom hook.<\/p>\n\n<h2>\n  \n  \n  Sample\n<\/h2>\n\n<h3>\n  \n  \n  useDebounce Hook\n<\/h3>\n\n<p><em>Tip<\/em> : Debounce delays invoking a function until after wait milliseconds have elapsed since the last time the debounced function was invoked.<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>import { useEffect, useState } from 'react'\n\nfunction useDebounce&lt;T&gt;(value: T, delay?: number): T {\n  const [debouncedValue, setDebouncedValue] = useState&lt;T&gt;(value)\n\n  useEffect(() =&gt; {\n    const timer = setTimeout(() =&gt; setDebouncedValue(value), delay || 500)\n\n    return () =&gt; {\n      clearTimeout(timer)\n    }\n  }, [value, delay])\n\n  return debouncedValue\n}\n\nexport default useDebounce\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>By using return function of <code>useEffect<\/code> after changing <code>value<\/code> or <code>delay<\/code>, React clear the timer identifier and function inside <code>setTimeout<\/code> not going to run. So it debounces. \ud83d\udc4c<\/p>\n\n<h3>\n  \n  \n  Usage\n<\/h3>\n\n\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>import React, { ChangeEvent, useEffect, useState } from 'react'\n\nimport { useDebounce } from 'usehooks-ts'\n\nexport default function Component() {\n  const [value, setValue] = useState&lt;string&gt;('')\n  const debouncedValue = useDebounce&lt;string&gt;(value, 500)\n\n  const handleChange = (event: ChangeEvent&lt;HTMLInputElement&gt;) =&gt; {\n    setValue(event.target.value)\n  }\n\n  \/\/ Fetch API (optional)\n  useEffect(() =&gt; {\n    \/\/ Do fetch here...\n    \/\/ Triggers when \"debouncedValue\" changes\n  }, [debouncedValue])\n\n  return (\n    &lt;div&gt;\n      &lt;p&gt;Value real-time: {value}&lt;\/p&gt;\n      &lt;p&gt;Debounced value: {debouncedValue}&lt;\/p&gt;\n\n      &lt;input type=\"text\" value={value} onChange={handleChange} \/&gt;\n    &lt;\/div&gt;\n  )\n}\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p><em>Credit: useHooks-ts<\/em> <a href=\"https:\/\/usehooks-ts.com\/react-hook\/use-debounce\">*<\/a><\/p>\n\n<h2>\n  \n  \n  Libraries\n<\/h2>\n\n<p>You do not have to build common scenarios from scratch. There are fantastic packages out there you can quickly learn and use. Here I listed some reliable players out of sort.<\/p>\n\n<p>react-use <a href=\"https:\/\/github.com\/streamich\/react-use\">*<\/a> \u2b50\ufe0f 29.6k \/ \ud83d\udd3d 958k<br>\nahooks <a href=\"https:\/\/github.com\/alibaba\/hooks\">*<\/a> \u2b50\ufe0f 9.1k \/ \ud83d\udd3d 74k<br>\nawesome-react-hooks <a href=\"https:\/\/github.com\/rehooks\/awesome-react-hooks\">*<\/a> \u2b50\ufe0f 8.1k \/ \ud83d\udd3d 74k<br>\nusehooks-ts <a href=\"https:\/\/github.com\/juliencrn\/usehooks-ts\">*<\/a> \u2b50\ufe0f 1.6k \/ \ud83d\udd3d 44k <br>\nreact-recipes <a href=\"https:\/\/github.com\/craig1123\/react-recipes\">*<\/a> \u2b50\ufe0f 850 \/ \ud83d\udd3d 1.2k<br>\nreact-hanger <a href=\"https:\/\/github.com\/kitze\/react-hanger\">*<\/a> \u2b50\ufe0f 1.8k \/ \ud83d\udd3d 3.3k<\/p>\n\n<h2>\n  \n  \n  Refrences\n<\/h2>\n\n<p>React offcial doc <a href=\"https:\/\/reactjs.org\/docs\/hooks-intro.html\">*<\/a><\/p>\n\n","category":["react","designpattern","javascript","codequality"]},{"title":"Beginner\u2019s guide to Tailwind CSS: the new way of styling","pubDate":"Mon, 04 Oct 2021 17:21:54 +0000","link":"https:\/\/dev.to\/rezab\/beginner-s-guide-to-tailwind-css-the-new-way-of-styling-3i80","guid":"https:\/\/dev.to\/rezab\/beginner-s-guide-to-tailwind-css-the-new-way-of-styling-3i80","description":"<h2>\n  \n  \n  Intro\n<\/h2>\n\n<p>Tailwindcss is a utility-first CSS framework which can be assumed as an API for your design system.<\/p>\n\n<blockquote>\n<p>Utility classes help you work within the constraints of a system instead of littering your stylesheets with arbitrary values. They make it easy to be consistent with color choices, spacing, typography, shadows, and everything else that makes up a well-engineered design system.<\/p>\n<\/blockquote>\n\n<p>In this article I'm going to share some experiences I've achieved during work with this incredible CSS framework lately.<\/p>\n\n<h2>\n  \n  \n  Learning\n<\/h2>\n\n<p>Although it is a must-do to read thoroughly each and every framework's documentation, it is rare to find a nice one.<br>\nTailwindcss documentation is one of the best out there. Easy to navigate, well-balanced descriptions and beautiful.<br><br>\nDefinitely you should check it out and learn everything you need to know. (<a href=\"https:\/\/tailwindcss.com\/docs\" rel=\"noopener noreferrer\">+<\/a>)<\/p>\n<h2>\n  \n  \n  An unbiased review\n<\/h2>\n\n<p>Moreover learning, you might need to know how Tailwind CSS introduce itself to the community. Which one is better? Bootstrap or Tailwind? Is it right time to switch?\ud83e\udd14<br>\nKyle has a short and informative video about it. (<a href=\"https:\/\/youtu.be\/hdGsFpZ0J2E\" rel=\"noopener noreferrer\">+<\/a>)<\/p>\n<h2>\n  \n  \n  Cheatsheet\n<\/h2>\n\n<p>In fact, we shouldn't memorize things that can be available in a blink of an eye. \ud83d\ude0e<br>\nCheatsheets can play huge role in our daily life and we all love it. <br>\nYou can check out an amazing cheatsheet for Tailwind CSS here. (<a href=\"https:\/\/umeshmk.github.io\/Tailwindcss-cheatsheet\/\" rel=\"noopener noreferrer\">+<\/a>)<\/p>\n<h2>\n  \n  \n  Playground\n<\/h2>\n\n<p>Many developers are \"Learn by doing\" people. Tailwind Play is an interactive environment that you can right jump into practicing and learning Tailwind. (<a href=\"https:\/\/play.tailwindcss.com\/\" rel=\"noopener noreferrer\">+<\/a>)<br>\nAdditionally, it's worth to play <a href=\"https:\/\/knightsoftheflexboxtable.com\/\" rel=\"noopener noreferrer\">this game<\/a>, too. Apply Tailwind knowledge and Flex to navigate the knight through the dungeon. \ud83d\ude09<\/p>\n<h2>\n  \n  \n  My Todo project\n<\/h2>\n\n<p>Last but not least, review a sample project give you great insight. Check out my simple Todo App built with React, Typescript and Tailwind CSS.<\/p>\n\n<p><a href=\"https:\/\/media.dev.to\/dynamic\/image\/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto\/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuhpwu4inpbs86jiw2tp1.png\" class=\"article-body-image-wrapper\"><img src=\"https:\/\/media.dev.to\/dynamic\/image\/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto\/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuhpwu4inpbs86jiw2tp1.png\" alt=\"image\"><\/a><\/p>\n\n\n<div class=\"ltag-github-readme-tag\">\n  <div class=\"readme-overview\">\n    <h2>\n      <img src=\"https:\/\/media.dev.to\/dynamic\/image\/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto\/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg\" alt=\"GitHub logo\">\n      <a href=\"https:\/\/github.com\/reza899\" rel=\"noopener noreferrer\">\n        reza899\n      <\/a> \/ <a href=\"https:\/\/github.com\/reza899\/todo-app\" rel=\"noopener noreferrer\">\n        todo-app\n      <\/a>\n    <\/h2>\n    <h3>\n      simple Todo App built with React, Typescript and TailwindCss\n    <\/h3>\n  <\/div>\n<\/div>\n\n\n<h2>\n  \n  \n  Conclusion\n<\/h2>\n\n<p>From my point of view, learning Tailwind is one the things every web developer should go with it. I believe it is like a revolution in styling world.<br>\nBy using Tailwind build rapidly and much easier styling. Also you're not going to write vanilla CSS anymore.<\/p>\n\n<p>If you have any other recommendations, let everyone know in the comments.<\/p>\n\n","category":["tailwindcss","css","webdev"]},{"title":"Data Loading with SSR in React","pubDate":"Mon, 20 Sep 2021 07:26:39 +0000","link":"https:\/\/dev.to\/rezab\/data-loading-with-ssr-in-react-185p","guid":"https:\/\/dev.to\/rezab\/data-loading-with-ssr-in-react-185p","description":"<h2>\n  \n  \n  Intro\n<\/h2>\n\n<p>In this article I want to check out how data loading works in SSR.<\/p>\n\n<p>Note: you can access source code of this article <a href=\"https:\/\/github.com\/reza899\/ssr-data-load\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n\n<h2>\n  \n  \n  What is Server-side Rendering(SSR) ? \ud83e\udd14\n<\/h2>\n\n<p>With server-side rendering, the server is the one that takes care of running our scripts and rendering all of our elements. (see <a href=\"https:\/\/dev.to\/alain2020\/ssr-vs-csr-2617\">this<\/a> describe thoroughly about SSR and differences with CSR)<\/p>\n\n<h2>\n  \n  \n  Setting up a simple server\n<\/h2>\n\n<p>First of all, we need to have a server. By using classic CRA, installing few packages and adding two files below in the root folder simply we can have a express server.<\/p>\n\n<p>To take modern code and convert that into something which is understandable for Nodejs should use babel and it's related presets: <br>\n<code>npm i -D @babel\/core @babel\/preset-env @babel\/preset-react @babel\/node nodemon<\/code><\/p>\n\n<p>Then, install express to serve our app:<br>\n<code>npm i express<\/code><\/p>\n\n<p>We also need an optimized production build by running <code>npm run build<\/code>. <\/p>\n\n<p><code>.babelrc<\/code> file:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>{\n  \"presets\": [\"@babel\/preset-env\", \"@babel\/preset-react\"]\n}\n<\/code><\/pre>\n\n<\/div>\n\n\n<p><code>server.js<\/code> file:<br>\n<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>import express from \"express\";\nimport React from \"react\";\nimport { renderToString } from \"react-dom\/server\";\nimport path from \"path\";\nimport fs from \"fs\";\nimport App from \".\/src\/App\";\n\nconst app = express();\n\napp.use(express.static(\".\/build\", { index: false }));\n\napp.get(\"\/*\", (req, res) =&gt; {\n  const reactApp = renderToString(&lt;App \/&gt;);\n\n  const templateFile = path.resolve(\".\/build\/index.html\");\n  fs.readFile(templateFile, \"utf8\", (err, data) =&gt; {\n    if (err) {\n      return res.status(500).send(err);\n    }\n\n    return res.send(\n      data.replace('&lt;div id=\"root\"&gt;&lt;\/div&gt;', `&lt;div id=\"root\"&gt;${reactApp}&lt;\/div&gt;`)\n    );\n  });\n});\n\napp.listen(8080, () =&gt; {\n  console.log(\"Server is listening on port 8080\");\n});\n\n<\/code><\/pre>\n\n<\/div>\n\n\n<p>For running the server you can use <code>nodemon<\/code> in order to restart after a change automatically:<br>\n<code>npx nodemon --exec npx babel-node server.js<\/code><\/p>\n<h2>\n  \n  \n  When should we load data?\n<\/h2>\n\n<p>I'm going to look at how data loading works with regards to server-side rendered applications.<\/p>\n\n<p>To show data loading with server-side rendering let's add some coins \ud83d\udcb2 to our server. <\/p>\n\n<p>In <code>server.js<\/code> file I'm going to add a special API route that our front-end will be able to use to load the coins data.<br>\n<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>const coins = [\n    { name: \"Bitcoin\", price: \"47000\" },\n    { name: \"Ethereum\", price: \"3300\" },\n    { name: \"cardano\", price: \"3\" },\n  ];\n\n  app.get(\"\/api\/coins\", (req, res) =&gt; {\n    const loadedCoins = coins;\n    res.json(loadedCoins);\n  });\n<\/code><\/pre>\n\n<\/div>\n\n\n<p>What I'm going to do, is just send back to the client some fake coins that will define in an array. Normally, you'd want to read these from a database or API (e.g. <a href=\"https:\/\/rapidapi.com\/Coinranking\/api\/coinranking1\/\" rel=\"noopener noreferrer\">RapidAPI<\/a>), but just for simplicity sake, I'll define them right in our source code. <\/p>\n\n<p>So our front-end now will be able to load the coins from this endpoint. So let's load our data inside a useEffect hook.<br>\n<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>function App() {\n  const [coins, setCoins] = useState();\n\n  useEffect(() =&gt; {\n    fetch(\"\/api\/coins\")\n      .then((res) =&gt; res.json())\n      .then((data) =&gt; setCoins(data));\n  }, []);\n\n  return (\n    &lt;&gt;\n      &lt;h1&gt;Server-side Rendering&lt;\/h1&gt;\n      &lt;ul&gt;\n        {coins &amp;&amp;\n          coins.map((coin, i) =&gt; {\n            return (\n              &lt;li key={i}&gt;\n                {coin.name} - {coin.price}\n              &lt;\/li&gt;\n            );\n          })}\n      &lt;\/ul&gt;\n    &lt;\/&gt;\n  );\n}\n<\/code><\/pre>\n\n<\/div>\n\n\n<p>Let's build our front-end by <code>npm run build<\/code>.<\/p>\n\n<p>If we inspect the HTML that we got back from the server, what we're going to see does not actually include the coins. why? \ud83d\ude2e<\/p>\n\n<p>What's happening is, the server is server-side rendering the front-end, except for the parts where we need to load data.\ud83d\ude09<\/p>\n\n\n<div class=\"ltag-github-readme-tag\">\n  <div class=\"readme-overview\">\n    <h2>\n      <img src=\"https:\/\/media.dev.to\/dynamic\/image\/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto\/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg\" alt=\"GitHub logo\">\n      <a href=\"https:\/\/github.com\/reza899\" rel=\"noopener noreferrer\">\n        reza899\n      <\/a> \/ <a href=\"https:\/\/github.com\/reza899\/ssr-data-load\" rel=\"noopener noreferrer\">\n        ssr-data-load\n      <\/a>\n    <\/h2>\n    <h3>\n      how data loading works with regards to server-side rendered applications\n    <\/h3>\n  <\/div>\n<\/div>\n\n\n\n","category":["react","javascript","webdev","ssr"]},{"title":"Time and its management tools","pubDate":"Fri, 09 Jul 2021 15:42:30 +0000","link":"https:\/\/dev.to\/rezab\/time-and-its-management-tools-2e4","guid":"https:\/\/dev.to\/rezab\/time-and-its-management-tools-2e4","description":"<h3>\n  \n  \n  Introduction\n<\/h3>\n\n<p>We have tried to reduce the cost of doing things with different methods and tools with the aim of improving processes during our lifelong.<br>\nEveryone with expertise in a certain field can live a balanced life and also benefit from other specialties and services without its scientific knowledge.<\/p>\n\n<p>In the field of talent identification, one of the criteria for measuring talent is definitely time. \"Time\" determine who is more capable than the others. In fact, if unlimited time is given to any person, he\/she will probably achieve the desired results and output.<\/p>\n\n<p>So time is clearly one of the most important assets of ours. Such a significant thing will lead to negative returns due to mismanagement or neglect. Therefore, it would be logical to choose a goal based on the principles of SMART (<a href=\"https:\/\/en.wikipedia.org\/wiki\/SMART_criteria\">+<\/a>) and find the meaning of our life more quickly and in less time with self-control, which includes time management, and approach to the top of the need hierarchy pyramid.<\/p>\n\n<h3>\n  \n  \n  Time management mistakes\ud83e\udd14\n<\/h3>\n\n<p>First of all, let's review some important mistakes(pitfalls) about time management.<\/p>\n\n<p>1- Sleep less as an option to increase time.<br>\n2- Prepare a long and detailed list of things to do.<br>\n3- Use several different tools and techniques to manage time.<br>\n4- Do several things at once.<br>\n5- Plan for all the moments and manage the seconds.<br>\n6- Continuous work.<br>\n7- Have an obsession to do all things.<\/p>\n\n<p><em>Note<\/em>: Although there are much more things you can add it to this list, these items are less considered.<\/p>\n\n<h3>\n  \n  \n  Time management tools \ud83d\udca5\n<\/h3>\n\n<p>First, time management does not require any special tools. It is really possible to have the best time management method with just a pen and paper. Perhaps the most important feature of time management tools is the simplification of task management and the reduction of related concerns. For example, it is difficult to set a reminder at a specific time on plain paper.<\/p>\n\n<p>Personally, I am comfortable with Mr. Allen's GTD method (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Getting_Things_Done\">+<\/a>), and of course I got the idea from it and implemented it in my own style.<\/p>\n\n<p>There are a plenty of time-management related tools out there that can makes you confusion (watch <a href=\"https:\/\/www.ted.com\/talks\/barry_schwartz_the_paradox_of_choice\">Mr. Schwartz's The paradox of choice<\/a>), and the purpose of this post is to introduce a few tools that may be helpful for you.<\/p>\n\n<h3>\n  \n  \n  List of preferred tools\n<\/h3>\n\n<p>Google Keep (<a href=\"https:\/\/keep.google.com\/\">+<\/a>)<br>\nTrello (<a href=\"https:\/\/trello.com\/\">+<\/a>)<\/p>\n\n<p>In Keep you can have different notes (photo, plain text, checkbox, voice and drawing) in different colors and categories. An extraordinary flexibility on which any kind of mentality can be applied. If you can work with it continuously for two weeks, you will find your style. You can start with Mr. Allen's idea. (e.g. Having an \"inbox\" of all to-dos)<\/p>\n\n<p>One of the great advantages of Keep is that when you are in a brainstorm (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Brainstorming\">+<\/a>), you can immediately record your voice in Keep or take a picture of your current position as a note by the phone's camera. That way, your ideas will not be forgotten.<\/p>\n\n","category":["productivity","time","softskill"]},{"title":"Async\/Await in JavaScript","pubDate":"Fri, 18 Jun 2021 14:45:13 +0000","link":"https:\/\/dev.to\/rezab\/async-await-in-javascript-noe","guid":"https:\/\/dev.to\/rezab\/async-await-in-javascript-noe","description":"<p>Async functions are brought to JavaScript by ES8 (ES2017) and are used to facilitate the management of asynchronous operations. Async functions use Promises under their own skin.<\/p>\n\n<p>To use this feature to manage an asynchronous operation, we first use the <code>async<\/code> keyword when defining a function. We always write the word <code>async<\/code> at the beginning of the function definition:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>const users = async () =&gt;{ }\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>When we use <code>async<\/code>, we have another keyword called <code>await<\/code>. When the <code>await<\/code> keyword is at the beginning of a expression, our code waits for the output of that expression to be specified and then goes to the next lines. We use await as follows:<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>const users = async () =&gt; {\n     let users = await getUsers();\n     console.log(users);\n}\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>One thing to keep in mind is that the <code>await<\/code> keyword should only be used within a function that uses the <code>async<\/code> keyword at the beginning. Otherwise we get an error.<\/p>\n\n<p>The next point to keep in mind is that if the expression in front of the <code>await<\/code> is not a Promise, it will automatically become a resolved Promise when it is done.<\/p>\n\n<p>The output of an async function is always a Promise and we can treat it like a Promise.<br>\n<\/p>\n\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>const users = async () =&gt;{\n   let users = await getUsers();\n   return users;\n}\n\nusers.then(console.log); \/\/list of users\n<\/code><\/pre>\n\n<\/div>\n\n\n\n<p>P.S.: Async code is simpler and more readable than the Promises we had to use with chain methods.<\/p>\n\n","category":["async","await","promise","javascript"]}]}}