Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

reactjs - React-date-picker activeStartDate not working

I am using react-date-picker library in my project. I need to display placeholder in datepicker instead of already chosen date. I've managed to achieve this

enter image description here

but it seems that when user opens calendar it doesn't display any active day enter image description here.

I would like that to be by default today. I've found in react-calendar library that there is a prop activeStartDate which should do exactly what I need, but for some reason it does not work. What do I do wrong?

import React, { useState } from "react";
import DatePicker from "react-date-picker";
import SVGIcon from "components/SVGIcon/SVGIcon";
import "./date-picker.scss";

const Calendar = () => {
  const [value, onChange] = useState(null);

  let today = new Date();

  return (
    <div className="wrapper">
      <DatePicker
        onChange={onChange}
        value={value}
        calendarIcon={<SVGIcon name="calendar" />}
        prevLabel={<SVGIcon name="buttonArrow" />}
        nextLabel={<SVGIcon name="buttonArrow" />}
        locale="en-GB"
        dayPlaceholder="day"
        monthPlaceholder="month"
        yearPlaceholder="year"
        activeStartDate={today}
      />
    </div>
  );
};

export default Calendar;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When initialising your state const [value, onChange] = useState(null); you set value as null then you pass it to <DatePicker value={value} />

If you want to pass today's date by default you can set value to today and that should do the trick: const [value, onChange] = useState(new Date());


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...