javascript - how to disable some dates in react-calendar - Stack Overflow

Could someone tell me how to pass an array of disabled dates to the calendar?I searched but couldn

Could someone tell me how to pass an array of disabled dates to the calendar?

I searched but couldn't find how to do it

import React, { useState, useEffect} from 'react';
import Api from '../../services/api';
import Calendar from 'react-calendar';
import { useParams } from "react-router-dom";
import 'react-calendar/dist/Calendar.css';

function MyApp() {
  const { id } = useParams();
  const [value, onChange] = useState(new Date());
  const [disabledDates, setDisabledDates] = useState([]);

  useEffect(() => {
    loadDisabledDates();
  }, []);

  function loadDisabledDates()
  {
    Api
      .get("/dates/"+id)
      .then((response) => {
        setDisabledDates(response.data);
      })
      .catch((err) => {
        console.error("error: " + err);
      });
  }

  return (
    <div>
      <Calendar onChange={onChange} value={value} />
    </div>
  );
}

Could someone tell me how to pass an array of disabled dates to the calendar?

I searched but couldn't find how to do it

https://www.npmjs./package/react-calendar

import React, { useState, useEffect} from 'react';
import Api from '../../services/api';
import Calendar from 'react-calendar';
import { useParams } from "react-router-dom";
import 'react-calendar/dist/Calendar.css';

function MyApp() {
  const { id } = useParams();
  const [value, onChange] = useState(new Date());
  const [disabledDates, setDisabledDates] = useState([]);

  useEffect(() => {
    loadDisabledDates();
  }, []);

  function loadDisabledDates()
  {
    Api
      .get("/dates/"+id)
      .then((response) => {
        setDisabledDates(response.data);
      })
      .catch((err) => {
        console.error("error: " + err);
      });
  }

  return (
    <div>
      <Calendar onChange={onChange} value={value} />
    </div>
  );
}
Share Improve this question asked Mar 28, 2022 at 0:56 PerrechilPerrechil 91 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Going off the other answers, here is how to use Array.includes() method to check against many dates

import { FC, useEffect, useState } from 'react'
import Calendar from 'react-calendar'
import { add, format } from "date-fns";


interface iProps {
  values: any | undefined,
  setValues: any,
  times: string[] | undefined,
  setTimes: any,
}


const bDate1 = new Date('2023-05-25').getDate()
const bDate2 = new Date('2023-05-26').getDate()
const bDate3 = new Date('2023-05-27').getDate()

const blackoutDates = [bDate1, bDate2, bDate3]

export const CalendarTime: FC<iProps> = ({ values, setValues, times,  setTimes }) => {


  return (


      <Calendar
        minDate={new Date()}
        className={'REACT-CALENDAR p-2'}
        view='month'
        onClickDay={(date) => {
          setValues((prev:any) => ({...prev, date: format(date, 'yyyy-MM-dd')}))

        }}
        tileDisabled={({date}) => blackoutDates.includes(date.getDate()) }
      // value={date}
      />


  )
}
import React from "react"
import Calendar from 'react-calendar'
export default function Slots(){
const disableDates = new Date('August 19, 2022 23:15:30');
const date1=disableDates.getDate();

return(
<div className="calendar">
<Calendar
 tileDisabled={({date}) => date.getDate()===date1}
/>
</div>
)
}

Edit: Scrolling further down the documentation, there is also a prop called tileDisabled. So that's probably your answer.

First answer: It looks, from the documentation, like your best bet is use the available props onClickDay, onClickMonth, etc

const handleClickDay = (e) => {
    if(disableDates.includes(e.target.value)){
        alert('this date is disabled')
    }else{
        //do something with the date
    }
}
return(
    <Calendar onChange={onChange} value={value}
        onClickDay={handleClickDay} />
)

I haven't used this library, so I'm not sure e.target.value will give you exactly the data but it should be something like this.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745336181a4623115.html

相关推荐

  • javascript - how to disable some dates in react-calendar - Stack Overflow

    Could someone tell me how to pass an array of disabled dates to the calendar?I searched but couldn

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信