<?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: Esmatullah Niazi. ོོ</title>
    <description>The latest articles on DEV Community by Esmatullah Niazi. ོོ (@esmat0100).</description>
    <link>https://dev.to/esmat0100</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%2F1013279%2F8e1c918d-3c64-41cb-8972-4b0c4869ff45.jpg</url>
      <title>DEV Community: Esmatullah Niazi. ོོ</title>
      <link>https://dev.to/esmat0100</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/esmat0100"/>
    <language>en</language>
    <item>
      <title>Building Blocks of Success: Mastering Reusable Components in ReactJS with Props, State, and Testing</title>
      <dc:creator>Esmatullah Niazi. ོོ</dc:creator>
      <pubDate>Thu, 02 Feb 2023 17:06:58 +0000</pubDate>
      <link>https://dev.to/esmat0100/building-blocks-of-success-mastering-reusable-components-in-reactjs-with-props-state-and-testing-5f77</link>
      <guid>https://dev.to/esmat0100/building-blocks-of-success-mastering-reusable-components-in-reactjs-with-props-state-and-testing-5f77</guid>
      <description>&lt;p&gt;Welcome to the world of &lt;strong&gt;reusable components&lt;/strong&gt;! In this blog post, we’ll be discussing the benefits of using reusable components in &lt;strong&gt;ReactJS&lt;/strong&gt; and how they can be created and managed. So, put on your favorite stickers, grab a snack, and let’s dive in!&lt;/p&gt;

&lt;p&gt;First things first, &lt;strong&gt;what are reusable components?&lt;/strong&gt; Simply put, reusable components are pieces of code that can be used multiple times throughout an application. They’re like the ultimate Lego pieces for your code. They save you time, make your code more modular, and make it easier to maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reusable components&lt;/strong&gt; are like the ultimate Lego pieces for your code. They save you time, make your code more modular, and make it easier to maintain.&lt;/p&gt;

&lt;p&gt;Using reusable components has many benefits. For one, it makes your code more modular, which means that it’s easier to understand and maintain. Each component only has to worry about its own functionality, which makes it easier to make changes or add new features. It also helps to keep your code DRY (Don’t Repeat Yourself), which reduces the chances of bugs and makes it easier to update your application.&lt;/p&gt;

&lt;p&gt;Here’s an example of a reusable button component:&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 from 'react';

const Button = ({ label }) =&amp;gt; {
  return (
    &amp;lt;button&amp;gt;{label}&amp;lt;/button&amp;gt;
  );
}

export default Button;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a simple button component that takes in a label prop and displays that label on the button. Now, we can use this button component anywhere in our application, making it easy to reuse and maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reusable components&lt;/strong&gt; can also be managed using props and state. &lt;strong&gt;Props&lt;/strong&gt; are used to pass data from a parent component to a child component, and &lt;strong&gt;state&lt;/strong&gt; is used to manage the data and behavior of a component. Here’s an example of a reusable text input component that uses props and state:&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, { useState } from 'react';

const TextInput = ({ label }) =&amp;gt; {
  const [value, setValue] = useState('');

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;label&amp;gt;{label}&amp;lt;/label&amp;gt;
      &amp;lt;input
        type="text"
        value={value}
        onChange={event =&amp;gt; setValue(event.target.value)}
      /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default TextInput;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a text input component that takes in a label prop and displays that label as the input’s label. We also use the &lt;strong&gt;useState&lt;/strong&gt; hook to manage the value state of the input, which allows the component to update its own value as the user types.&lt;/p&gt;

&lt;p&gt;Another great thing about &lt;strong&gt;reusable components&lt;/strong&gt; is that they can be composed to create more complex components. For example, you can use a reusable button component and a reusable text input component to create a reusable form component. This allows you to break down your &lt;strong&gt;application into smaller, more manageable pieces, making it easier to understand and maintain&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here’s an example of a reusable form component that uses the button and text input components from the previous examples:&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, { useState } from 'react';
import Button from './Button';
import TextInput from './TextInput';

const Form = () =&amp;gt; {
  const [formData, setFormData] = useState({});

  const handleSubmit = () =&amp;gt; {
    console.log(formData);
  }

  return (
    &amp;lt;form onSubmit={handleSubmit}&amp;gt;
      &amp;lt;TextInput
        label="Name"
        value={formData.name}
        onChange={value =&amp;gt; setFormData({ ...formData, name: value })}
      /&amp;gt;
      &amp;lt;TextInput
        label="Email"
        value={formData.email}
        onChange={value =&amp;gt; setFormData({ ...formData, email: value })}
      /&amp;gt;
      &amp;lt;Button label="Submit" onClick={handleSubmit} /&amp;gt;
    &amp;lt;/form&amp;gt;
  );
}

export default Form;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a form component that uses the reusable button and text input components. We also use the useState hook to manage the formData state, which allows the component to update its own form data as the user interacts with the form. We also added a submit button and handleSubmit function that logs the form data when the form is submitted.&lt;/p&gt;

&lt;p&gt;Another way to manage &lt;strong&gt;reusable components&lt;/strong&gt; is to use component libraries. There are many popular component libraries such as &lt;em&gt;&lt;strong&gt;Material-UI, Ant Design, and Semantic UI React&lt;/strong&gt;&lt;/em&gt; that provide a wide range of pre-built, reusable components. These libraries can save you a lot of time, as you don’t have to build all the components from scratch. They also follow design guidelines, which makes it easy to make your application look polished and professional.&lt;/p&gt;

&lt;p&gt;Another advantage of using reusable components is that it helps with testing. By breaking down your application into smaller, reusable components, you can more easily test each component individually. This makes it easier to identify and fix bugs, and also helps to ensure that new changes don’t break existing functionality. React also has a great testing library called Jest that makes it easy to write and run tests for your components.&lt;/p&gt;

&lt;p&gt;Here’s an example of a test for the reusable button component:&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 from 'react';
import { render, fireEvent } from '@testing-library/react';
import Button from './Button';

test('Button component should display the label text', () =&amp;gt; {
  const label = 'Click me';
  const { getByText } = render(&amp;lt;Button label={label} /&amp;gt;);
  const button = getByText(label);

  expect(button).toBeInTheDocument();
});

test('Button component should call onClick function when clicked', () =&amp;gt; {
  const onClick = jest.fn();
  const { getByText } = render(&amp;lt;Button label="Click me" onClick={onClick} /&amp;gt;);
  const button = getByText('Click me');

  fireEvent.click(button);
  expect(onClick).toHaveBeenCalled();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we use @testing-library/react to render our button component and test that it displays the correct label text and calls the onClick function when clicked. By writing tests for each reusable component, you can ensure that each component is functioning correctly and that changes to one component do not break the functionality of the other components.&lt;/p&gt;

&lt;p&gt;In addition to the benefits of reusable components for development, it also has a positive impact on design. By creating reusable components, you can ensure that your application has a consistent look and feel. This makes it easier for users to understand and navigate your application, and also helps to create a more professional and polished look.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In conclusion, reusable components are a powerful tool in ReactJS that can help you build more maintainable, modular, efficient and consistent code. By breaking down your application into smaller, reusable components, you can easily test each component individually and ensure that your application has a consistent look and feel. Reusable components can also be managed with the help of props and state, and component libraries can save development time and provide a polished and professional look. Additionally, testing libraries like Jest can aid in catching bugs and ensuring the functionality of your reusable components. By mastering reusable components, you can take your coding skills to the next level and build successful applications.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>crypto</category>
      <category>cryptocurrency</category>
      <category>blockchain</category>
      <category>web3</category>
    </item>
    <item>
      <title>ReactJS 101: A Beginner’s Guide to Building Reusable Components, Managing State, and Creating Real-World Applications</title>
      <dc:creator>Esmatullah Niazi. ོོ</dc:creator>
      <pubDate>Wed, 01 Feb 2023 21:52:53 +0000</pubDate>
      <link>https://dev.to/esmat0100/reactjs-101-a-beginners-guide-to-building-reusable-components-managing-state-and-creating-real-world-applications-534a</link>
      <guid>https://dev.to/esmat0100/reactjs-101-a-beginners-guide-to-building-reusable-components-managing-state-and-creating-real-world-applications-534a</guid>
      <description>&lt;p&gt;Welcome to the exciting world of &lt;strong&gt;ReactJS!&lt;/strong&gt; If you’re new to the world of &lt;strong&gt;JavaScript frameworks&lt;/strong&gt;, ReactJS is a great place to start. In this blog post, we’ll be introducing you to what ReactJS is, its main features, and how it differs from other JavaScript frameworks. So, grab a cup of coffee, sit back, and let’s dive in!&lt;/p&gt;

&lt;p&gt;First things first, what is ReactJS? Simply put, &lt;strong&gt;ReactJS is a JavaScript library for building user interfaces&lt;/strong&gt;. It’s used for building reusable UI components and managing the state of those components. ReactJS was developed and is maintained by Facebook, and it’s now widely used by companies such as Netflix, Airbnb, and Instagram.&lt;/p&gt;

&lt;p&gt;One of the main features of ReactJS is that it allows you to build &lt;strong&gt;reusable components&lt;/strong&gt;. Instead of building a new component for each page or feature, you can create a reusable component that can be used across multiple pages or features. This makes your code more modular and maintainable. Here’s an example of a reusable button component:&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 from 'react';

const Button = ({ label }) =&amp;gt; {
  return (
    &amp;lt;button&amp;gt;{label}&amp;lt;/button&amp;gt;
  );
}

export default Button;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a simple button component that takes in a label prop and displays that label on the button. Now, we can use this button component anywhere in our application, making it easy to reuse and maintain.&lt;/p&gt;

&lt;p&gt;Another key feature of ReactJS is its approach to &lt;strong&gt;state management&lt;/strong&gt;. In ReactJS, each component has its own state, which is an object that holds the data and behavior of that component. This allows for a more efficient way of managing the data and behavior of components, as opposed to managing it all in one global state. Here’s an example of a component that manages its own state:&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, { useState } from 'react';

const Counter = () =&amp;gt; {
  const [count, setCount] = useState(0);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default Counter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a simple counter component that uses the useState hook to manage its own count state. The count state is displayed on the page, and we also have a button that increments the count when clicked.&lt;/p&gt;

&lt;p&gt;Now, you may be wondering how ReactJS differs from other JavaScript frameworks. One of the main differences is that ReactJS focuses solely on building user interfaces, whereas other frameworks such as Angular and Vue also include features for building the overall structure of the application. ReactJS also emphasizes the use of reusable components, whereas other frameworks may have a more rigid component structure. Additionally, ReactJS has a more efficient approach to state management with its use of hooks.&lt;/p&gt;

&lt;p&gt;Another great feature of ReactJS is its ability to handle events. In ReactJS, you can attach &lt;strong&gt;event handlers to components&lt;/strong&gt;, which allows you to handle events such as clicks, mouseovers, and more. Here’s an example of a component that handles a click event:&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 from 'react';

const ClickHandler = () =&amp;gt; {
  const handleClick = () =&amp;gt; {
    console.log("Button was clicked!");
  }

  return (
    &amp;lt;button onClick={handleClick}&amp;gt;Click Me!&amp;lt;/button&amp;gt;
  );
}

export default ClickHandler;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a simple component that displays a button, and when the button is clicked, it will log a message to the console. This is just one example of how ReactJS makes it easy to handle events in a clear and concise way.&lt;/p&gt;

&lt;p&gt;Another great thing about ReactJS is that it can be used to build real-world applications. From simple to-do lists to complex e-commerce platforms, ReactJS is a great choice for any type of project. Here’s an example of a simple weather app built with ReactJS:&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, { useState, useEffect } from 'react';
import axios from 'axios';

const WeatherApp = () =&amp;gt; {
  const [weather, setWeather] = useState(null);

  useEffect(() =&amp;gt; {
    axios.get('https://api.openweathermap.org/data/2.5/weather?q=New+York&amp;amp;appid=YOUR_API_KEY')
      .then(response =&amp;gt; {
        setWeather(response.data);
      });
  }, []);

  if (!weather) {
    return &amp;lt;p&amp;gt;Loading weather...&amp;lt;/p&amp;gt;;
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Temperature: {weather.main.temp}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Weather: {weather.weather[0].main}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default WeatherApp;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a weather app that uses the useEffect hook to fetch data from an API and display the temperature and current weather for a specific location (in this case, New York). This is just one example of the many possibilities that ReactJS offers for building real-world applications.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;In conclusion, ReactJS is a powerful and popular JavaScript library for building user interfaces. Its main features include reusable components and efficient state management, which make it a great choice for building maintainable and performant applications. If you’re new to the world of JavaScript frameworks, ReactJS is a great place to start!&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>web3</category>
      <category>crypto</category>
      <category>offers</category>
    </item>
    <item>
      <title>Slide into Your Users’ Hearts ️ with Tailwind CSS and Headless UI in ReactJS</title>
      <dc:creator>Esmatullah Niazi. ོོ</dc:creator>
      <pubDate>Mon, 23 Jan 2023 11:49:16 +0000</pubDate>
      <link>https://dev.to/esmat0100/slide-into-your-users-hearts-with-tailwind-css-and-headless-ui-in-reactjs-20c0</link>
      <guid>https://dev.to/esmat0100/slide-into-your-users-hearts-with-tailwind-css-and-headless-ui-in-reactjs-20c0</guid>
      <description>&lt;p&gt;Are you tired of boring and static user interfaces? Want to add some flair and interactivity to your &lt;strong&gt;ReactJS app&lt;/strong&gt;? Look no further than using &lt;strong&gt;Tailwind CSS and Headless UI&lt;/strong&gt; to create &lt;strong&gt;slide-over components&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;First, let’s install the necessary packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @headlessui/react @tailwindcss/base
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we'll create a new component called &lt;strong&gt;SlideOver&lt;/strong&gt; that will contain the content we want to slide in and out. We'll use the &lt;strong&gt;HeadlessButton&lt;/strong&gt; component from Headless UI for the toggle button and apply some Tailwind classes for styling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { HeadlessButton } from '@headlessui/react'

const SlideOver = () =&amp;gt; {
  const [isOpen, setIsOpen] = useState(false)

  return (
    &amp;lt;div className="relative"&amp;gt;
      &amp;lt;HeadlessButton
        className="absolute top-0 right-0 m-4"
        onClick={() =&amp;gt; setIsOpen(!isOpen)}
      &amp;gt;
        {isOpen ? 'Close' : 'Open'}
      &amp;lt;/HeadlessButton&amp;gt;

      &amp;lt;div
        className={`absolute top-0 left-0 w-3/4 h-full bg-white p-4 rounded-lg shadow-lg transform transition-transform duration-300 ${
          isOpen ? 'translate-x-0' : '-translate-x-full'
        }`}
      &amp;gt;
        {/* Content goes here */}
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we’re using React’s useState hook to keep track of whether the slide-over is open or closed and toggle the state with the button. We're also using the transform and transition-transform classes from &lt;strong&gt;Tailwind CSS&lt;/strong&gt; to animate the slide-over in and out.&lt;/p&gt;

&lt;p&gt;Finally, let’s add some Tailwind classes to spice up the &lt;strong&gt;slide-over&lt;/strong&gt; content and make it look great:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { HeadlessButton } from '@headlessui/react'

const SlideOver = () =&amp;gt; {
  const [isOpen, setIsOpen] = useState(false)

  return (
    &amp;lt;div className="relative"&amp;gt;
      &amp;lt;HeadlessButton
        className="absolute top-0 right-0 m-4"
        onClick={() =&amp;gt; setIsOpen(!isOpen)}
      &amp;gt;
        {isOpen ? 'Close' : 'Open'}
      &amp;lt;/HeadlessButton&amp;gt;

      &amp;lt;div
        className={`absolute top-0 left-0 w-3/4 h-full bg-white p-4 rounded-lg shadow-lg transform transition-transform duration-300 ${
          isOpen ? 'translate-x-0' : '-translate-x-full'
        }`}
      &amp;gt;
        &amp;lt;h2 className="text-xl font-medium mb-4"&amp;gt;
          Welcome to the Slide-Over
        &amp;lt;/h2&amp;gt;
        &amp;lt;p className="text-gray-700 mb-4"&amp;gt;
          This is where you can put all your exciting content!
        &amp;lt;/p&amp;gt;
        &amp;lt;HeadlessButton className="bg-blue-500 text-white p-2 rounded-full"&amp;gt;
          Click me!
        &amp;lt;/HeadlessButton&amp;gt;
       &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it! A fully functional slide-over component using Tailwind CSS and Headless UI in ReactJS. The possibilities are endless with this combination of tools — you can customize the animation, layout, and design to make your slide-over truly unique.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Don’t be afraid to get creative and have fun with it! And remember, always slide responsibly.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: In a real-world scenario, you would want to make sure to handle the state of your component and also add some kind of overlay when the slideover is open to close it when clicked outside the slideover.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
