<?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: sagarkhadka</title>
    <description>The latest articles on DEV Community by sagarkhadka (@sagarkhadka).</description>
    <link>https://dev.to/sagarkhadka</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%2F903434%2F9816b42c-55f3-4fec-8e2c-4616314e3f04.jpeg</url>
      <title>DEV Community: sagarkhadka</title>
      <link>https://dev.to/sagarkhadka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sagarkhadka"/>
    <language>en</language>
    <item>
      <title>How to use Date-Range in react ?</title>
      <dc:creator>sagarkhadka</dc:creator>
      <pubDate>Wed, 21 Sep 2022 12:14:13 +0000</pubDate>
      <link>https://dev.to/sagarkhadka/how-to-use-date-range-in-react--3h2d</link>
      <guid>https://dev.to/sagarkhadka/how-to-use-date-range-in-react--3h2d</guid>
      <description>&lt;p&gt;Date-Range is a npm package that lets us to use and display modern looking calendar window on our website. To setup the Date-Range following steps&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; First do &lt;code&gt;npm i react-date-range&lt;/code&gt; install the package&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Import calendar along with its CSS file in the component file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Calendar, DateRange } from 'react-date-range'
import 'react-date-range/dist/styles.css'; // main style file
import 'react-date-range/dist/theme/default.css'; // theme css file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Create a useState for the calendar.&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 [date, setDate] = useState([
  {
    startDate: new Date(),
    endDate: new Date(), // Use null if you don't want to have endDate
    key: 'selection'
  }
])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; We have to also add numbers &lt;code&gt;npm add date-fns&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5:&lt;/strong&gt; Add following component with a className to position using CSS. Note: that props name should be same that we created in step 3. &lt;code&gt;const [date, setDate] = useState&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;DateRange editableDateInputs={true} onChange={(item) =&amp;gt; setDate([item.selection])} moveRangeOnFirstSelection={false} ranges={date} className="date" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 6:&lt;/strong&gt; If you want to show the selected date in format import ‘date-fns’&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { format } from 'date-fns';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 7:&lt;/strong&gt; To display selected date&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;/code&gt;${format(date[0].startDate, "MM/dd/yyyy")} to ${format(date[0].endDate, "MM/dd/yyy")}&lt;code&gt;}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above method only creates static calendar window. If we want to open date-range on click then add following code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt; Make a useState hook to open and close calendar window&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const [openDate, setOpenDate] = useState(false)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt; Wrap date component with &lt;code&gt;{openDate &amp;amp;&amp;amp; &amp;lt;DateRange /&amp;gt;}&lt;/code&gt; and set &lt;code&gt;onClick={()=&amp;gt;setOpenDate(!openDate)}&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Your final code should look like this&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 { DateRange } from 'react-date-range'
import 'react-date-range/dist/styles.css'; // main style file
import 'react-date-range/dist/theme/default.css'; // theme css file
import { format } from 'date-fns';

const Component = () =&amp;gt; {
  const [openDate, setOpenDate] = useState(false)
  const [date, setDate] = useState([
    {
      startDate: new Date(),
      endDate: new Date(),
      key: 'selection',
    }
  ])

  return (
    &amp;lt;div className='calendar'&amp;gt;
      &amp;lt;span onClick={ ()=&amp;gt;setOpenDate(!openDate) }&amp;gt;{ `${format(date[0].startDate, "MM/dd/yyyy")} to ${format(date[0].endDate, "MM/dd/yyy") }` }&amp;lt;/span&amp;gt;
      { openDate &amp;amp;&amp;amp; &amp;lt;DateRange editableDateInputs={ true } onChange={ (item) =&amp;gt; setDate([item.selection]) } moveRangeOnFirstSelection={ false } ranges={ date } className="date" /&amp;gt; }
    &amp;lt;/div&amp;gt;
  )
}

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

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
