<?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: Hastings Kondwani</title>
    <description>The latest articles on DEV Community by Hastings Kondwani (@thatmwcoder).</description>
    <link>https://dev.to/thatmwcoder</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%2F573606%2Fc180b070-31bd-4392-8786-9889f0143057.jpg</url>
      <title>DEV Community: Hastings Kondwani</title>
      <link>https://dev.to/thatmwcoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thatmwcoder"/>
    <language>en</language>
    <item>
      <title>Getting started with Zod</title>
      <dc:creator>Hastings Kondwani</dc:creator>
      <pubDate>Tue, 07 Feb 2023 12:36:44 +0000</pubDate>
      <link>https://dev.to/thatmwcoder/getting-started-with-zod-1il1</link>
      <guid>https://dev.to/thatmwcoder/getting-started-with-zod-1il1</guid>
      <description>&lt;p&gt;Zod is a powerful, flexible, and easy-to-use validation library that can be used with both TypeScript and JavaScript. It provides a simple, expressive syntax for defining complex validation schemas and can be easily integrated with popular frameworks such as Express. In this blog post, we'll take a closer look at what Zod is, how it works, and why you might choose it over other libraries like Yup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Zod?&lt;/strong&gt;&lt;br&gt;
Zod is a validation library that provides a simple, expressive syntax for defining validation schemas. It allows you to validate data structures and check if they match your defined schema. This is especially useful when working with user-generated data, such as from form submissions or API requests, to ensure that the data meets your application's requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Choose Zod?&lt;/strong&gt;&lt;br&gt;
There are several reasons why you might choose Zod over other validation libraries.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;TypeScript Support: Zod has first-class support for TypeScript, making it an excellent choice for TypeScript developers. It integrates seamlessly with TypeScript's type system, allowing you to leverage the benefits of static typing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flexible and Powerful Syntax: Zod's syntax is simple, yet powerful. It provides a wide range of validation types, including string, number, boolean, object, array, and more. It also supports custom validators, making it easy to implement complex validation rules.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Exception-Based Error Handling: Zod uses exceptions to signal validation errors, making it easier to handle errors in your application. When a validation error occurs, Zod will throw an exception that you can catch and handle as needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Great Performance: Zod is designed to be fast and efficient, and it provides great performance even for large datasets.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;How to Use Zod&lt;/strong&gt;&lt;br&gt;
Here's a simple example of how to use Zod with TypeScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as z from 'zod';

const userSchema = z.object({
  name: z.string().min(3).max(100),
  age: z.number().integer().min(18).max(100),
  email: z.string().email()
});

const validData = userSchema.validate({
  name: 'John Doe',
  age: 30,
  email: 'john@example.com'
});

console.log(validData);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we start by importing the Zod library. We then define a userSchema using Zod's syntax. The schema defines the structure of a user object, with constraints on the name, age, and email fields. Finally, we use the validate method to validate a sample user object and log the results to the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integrating Zod with Express&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Zod can be easily integrated with Express, a popular Node.js web framework. Here's an example of how you might use Zod to validate a user registration API endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import express from 'express';
import * as z from 'zod';

const app = express();
const userSchema = z.object({
  name: z.string().min(3).max(100),
  age: z.number().integer().min(18).max(100),
  email: z.string().email()
});

app.post('/register', (req, res) =&amp;gt; {
  try {
    const validData = userSchema.validate(req.body);
    // Continue with the registration process
  } catch (error) {
    res.status(400).send({ message: error.message });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we first import the Express and Zod libraries. We then define a userSchema using Zod's syntax. The schema defines the structure of a user object, with constraints on the name, age, and email fields. We then define a POST endpoint for registering a new user. In the endpoint, we use the validate method to validate the incoming request body against the userSchema. If the validation fails, an exception will be thrown, which we catch and respond to with a 400 Bad Request error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Zod is a powerful, flexible, and easy-to-use validation library that provides a simple and expressive syntax for defining validation schemas in TypeScript and JavaScript. It can be easily integrated with popular frameworks like Express, making it a versatile and convenient tool for validating user input in your application.&lt;/p&gt;

&lt;p&gt;My Github: &lt;a href="https://github.com/ThatMWCoder"&gt;https://github.com/ThatMWCoder&lt;/a&gt;&lt;br&gt;
My Linkedin: &lt;a href="https://www.linkedin.com/in/hastings-kondwani-9b5a92200/"&gt;https://www.linkedin.com/in/hastings-kondwani-9b5a92200/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Zod Official Docs: &lt;a href="https://zod.dev/"&gt;https://zod.dev/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>validation</category>
      <category>node</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Understanding the useRef hook</title>
      <dc:creator>Hastings Kondwani</dc:creator>
      <pubDate>Thu, 02 Feb 2023 14:07:31 +0000</pubDate>
      <link>https://dev.to/thatmwcoder/understanding-the-useref-hook-3bjg</link>
      <guid>https://dev.to/thatmwcoder/understanding-the-useref-hook-3bjg</guid>
      <description>&lt;p&gt;The useRef hook is one of the many hooks that React provides to allow developers to manage state and side effects in their applications. In this article, we will explore the useRef hook, its use cases, and how to implement it in a React application.&lt;/p&gt;

&lt;p&gt;What is the useRef Hook?&lt;/p&gt;

&lt;p&gt;The useRef hook creates a reference to an element in the DOM (Document Object Model). It returns an object that has a property called “current” which holds the reference to the element. The “current” property is mutable, meaning it can be updated with a new value whenever needed. The useRef hook provides a way to access and manipulate the DOM elements without triggering a re-render of the component.&lt;/p&gt;

&lt;p&gt;Use Cases of the useRef Hook&lt;/p&gt;

&lt;p&gt;Accessing DOM Elements: The useRef hook is used to access elements in the DOM directly, without using the state or props. This can be useful for creating animations, handling focus, or measuring elements.&lt;/p&gt;

&lt;p&gt;Improving Performance: The useRef hook can be used to improve performance by preventing unnecessary re-renders of a component. For instance, if a component’s value only needs to be updated once, the useRef hook can be used to store the value, and the component will only re-render when the value changes.&lt;/p&gt;

&lt;p&gt;How to use the useRef Hook&lt;/p&gt;

&lt;p&gt;The useRef hook is used inside a component and is called just like any other hook. The hook returns an object with a “current” property which holds the reference to the element. Here is an example of using the useRef hook to access an input element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useRef } from 'react';

function InputExample() {
  const inputRef = useRef(null);

  function handleClick() {
    inputRef.current.focus();
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;input type="text" ref={inputRef} /&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;Focus the input&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the useRef hook is used to create a reference to the input element. The reference is passed to the input element as a ref prop. The handleClick function is then used to focus the input element when the button is clicked.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;The useRef hook is a powerful tool that allows developers to access and manipulate the DOM elements in a React component. With its ability to improve performance by avoiding unnecessary re-renders, the useRef hook is a must-have in any React developer’s toolkit. Whether you need to access elements for animations or just for simple focus management, the useRef hook is the way to go.&lt;/p&gt;

&lt;p&gt;Linkedin: &lt;a href="https://www.linkedin.com/in/hastings-kondwani-9b5a92200/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/hastings-kondwani-9b5a92200/&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/ThatMWCoder" rel="noopener noreferrer"&gt;https://github.com/ThatMWCoder&lt;/a&gt;&lt;/p&gt;

</description>
      <category>web3</category>
      <category>blockchain</category>
      <category>crypto</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The basics of asynchronous JavaScript</title>
      <dc:creator>Hastings Kondwani</dc:creator>
      <pubDate>Fri, 27 Jan 2023 19:04:20 +0000</pubDate>
      <link>https://dev.to/thatmwcoder/the-basics-of-asynchronous-javascript-2798</link>
      <guid>https://dev.to/thatmwcoder/the-basics-of-asynchronous-javascript-2798</guid>
      <description>&lt;p&gt;Asynchronous JavaScript refers to the ability of JavaScript to execute code in a non-blocking way, allowing other code to run in the meantime. This is in contrast to synchronous JavaScript, which runs code in a blocking manner, where each line of code must be executed before moving on to the next.&lt;/p&gt;

&lt;p&gt;There are several ways to implement asynchronous JavaScript, including callbacks, Promises, and async/await.&lt;/p&gt;

&lt;p&gt;Callbacks:&lt;br&gt;
A callback is a function that is passed as an argument to another function, which is then called at a later time. Here's an example of using a callback to log the result of a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(a, b, callback) {
    setTimeout(() =&amp;gt; {
        callback(a + b);
    }, 1000);
}

add(1, 2, (result) =&amp;gt; {
    console.log(result); // 3
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the add() function takes in two numbers, a and b, and a callback function. The setTimeout() function is used to simulate a delay, and after one second, the callback function is called with the result of a + b.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Promises:&lt;/strong&gt;&lt;br&gt;
Promises are a more modern way to handle asynchronous code in JavaScript. A promise represents a value that may not be available yet, but will be at some point in the future. Here's an example of using a promise to log the result of a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(a, b) {
    return new Promise((resolve, reject) =&amp;gt; {
        setTimeout(() =&amp;gt; {
            resolve(a + b);
        }, 1000);
    });
}

add(1, 2)
    .then((result) =&amp;gt; {
        console.log(result); // 3
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the add() function returns a new promise that takes in two functions: resolve and reject. The setTimeout() function is used to simulate a delay, and after one second, the resolve() function is called with the result of a + b.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Async/await:&lt;/strong&gt;&lt;br&gt;
Async/await is a way to write asynchronous code that looks like synchronous code. It is built on top of promises and allows you to write asynchronous code using a more familiar syntax. Here's an example of using async/await to log the result of a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function add(a, b) {
    return new Promise((resolve, reject) =&amp;gt; {
        setTimeout(() =&amp;gt; {
            resolve(a + b);
        }, 1000);
    });
}

(async () =&amp;gt; {
    const result = await add(1, 2);
    console.log(result); // 3
})()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the add() function is declared as async and returns a promise. The await keyword is used to wait for the promise to resolve and get the result of the promise.&lt;/p&gt;

&lt;p&gt;These are some of the basic examples of how to use callbacks, Promises, and async/await in JavaScript. Each has its own advantages and use cases, and it's important to understand the difference and choose the right one for your project.&lt;/p&gt;

&lt;p&gt;Follow me on Linkedin: &lt;a href="https://www.linkedin.com/in/hastings-kondwani-9b5a92200/"&gt;https://www.linkedin.com/in/hastings-kondwani-9b5a92200/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>async</category>
      <category>promise</category>
    </item>
  </channel>
</rss>
