<?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: Vahid Afshari</title>
    <description>The latest articles on DEV Community by Vahid Afshari (@vahidinline).</description>
    <link>https://dev.to/vahidinline</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%2F798560%2F9253854b-8373-48ab-817e-6d00b1512d2b.jpeg</url>
      <title>DEV Community: Vahid Afshari</title>
      <link>https://dev.to/vahidinline</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vahidinline"/>
    <language>en</language>
    <item>
      <title>React CountDown component</title>
      <dc:creator>Vahid Afshari</dc:creator>
      <pubDate>Wed, 26 Jul 2023 12:58:31 +0000</pubDate>
      <link>https://dev.to/vahidinline/react-countdown-component-58jf</link>
      <guid>https://dev.to/vahidinline/react-countdown-component-58jf</guid>
      <description>&lt;p&gt;I just created a custom React countDown component and will share you my experience. In order to have a react countdown component that displays the remaining days, hours, and minutes, You'll need to set the target date for the countdown, and the component will calculate the time remaining and update it in real-time.&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';

const Countdown = ({ targetDate }) =&amp;gt; {
  const calculateTimeRemaining = () =&amp;gt; {
    const now = new Date().getTime();
    const target = new Date(targetDate).getTime();
    const timeRemaining = target - now;

    const days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
    const hours = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));

    return { days, hours, minutes };
  };

  const [timeRemaining, setTimeRemaining] = useState(calculateTimeRemaining());

  useEffect(() =&amp;gt; {
    const interval = setInterval(() =&amp;gt; {
      setTimeRemaining(calculateTimeRemaining());
    }, 1000);

    return () =&amp;gt; clearInterval(interval);
  }, []);

  return (
    &amp;lt;div&amp;gt;
      {timeRemaining.days} days, {timeRemaining.hours} hours, {timeRemaining.minutes} minutes remaining
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;In this component, I calculate the remaining days, hours, and minutes (adding seconds is easy) between the current time and the target date using the calculateTimeRemaining function. The setInterval inside the useEffect is used to update the time remaining every second (1000ms). The countdown will automatically update as time passes.&lt;/p&gt;

&lt;p&gt;To actually use this countdown component in your app, simply import and include it, passing the target date as a prop:&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 Countdown from './Countdown';

const App = () =&amp;gt; {
  const targetDate = new Date('2023-12-31T23:59:59'); // Replace this with your target date

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Countdown Timer&amp;lt;/h1&amp;gt;
      &amp;lt;Countdown targetDate={targetDate} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;Please replace the targetDate value with your desired date in the format YYYY-MM-DDTHH:mm:ss.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to collect multi-field data in React via form</title>
      <dc:creator>Vahid Afshari</dc:creator>
      <pubDate>Tue, 26 Jul 2022 15:06:00 +0000</pubDate>
      <link>https://dev.to/vahidinline/how-to-collect-form-data-in-react-e7p</link>
      <guid>https://dev.to/vahidinline/how-to-collect-form-data-in-react-e7p</guid>
      <description>&lt;p&gt;If you want to collect user data through a form from Front with React js, you may face to the this issue, How to store data in react and then send it to whatever, like your express abckend server.&lt;/p&gt;

&lt;p&gt;The first solution is to use useState() hook. It's not perfect but may help you in variues situations. But if you want to take several fields like name, email, birth date and so on, are you creating multi state? &lt;/p&gt;

&lt;p&gt;So, simply don't do that. the better solution is to use single hook to take user data as an object in react.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h3&gt;
  
  
  How?
&lt;/h3&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;p&gt;look at this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const [userAuth, setUserAuth] = useState({
    email: '',
    password: '',
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this would help you to store everything. no matter you want to get 2 inputs or 10, you only need a single useState().&lt;/p&gt;

&lt;p&gt;then you may ask you how to hanle input?&lt;br&gt;
you need a block of code like this. this function will handle your inputs and recognise them by their name and set the value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const handleInput = (e) =&amp;gt; {
    const { name, value } = e.target;
    setUserAuth({
      ...userAuth,
      [name]: value,
    });
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the only left thing is to assign handleInput to the input as a onchange event like this:&lt;br&gt;
&lt;code&gt;&lt;br&gt;
&amp;lt;Input type="email name="email" onChange={handleInput}/&amp;gt;&lt;br&gt;
&amp;lt;Input type="text name="name" onChange={handleInput}/&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;that's it. You then have an object will all user data&lt;/p&gt;

</description>
      <category>react</category>
      <category>form</category>
      <category>reactj</category>
    </item>
  </channel>
</rss>
