简体   繁体   English

使用 react-date-range 进行日期选择的月份跳跃

[英]Month jumping on date selection using react-date-range

I'm using DateRangePicker from react-date-range in a next.js project.我在next.js项目中使用来自react-date-range的 DateRangePicker。

You can see the expected behavior on their demo page : if you select any date from the month on the right, the months stay in place.您可以在他们的演示页面上看到预期的行为:如果您 select 右侧月份的任何日期,则月份保持不变。 Here's a video .这是一个视频

But in my project, the month from the right jumps to the left on date selection ( as you can see it in this video ).但是在我的项目中,从右边的月份在日期选择时跳到左边(正如你在这个视频中看到的那样)。
I've made a simple demo here by copying code from their demo page:我通过从他们的演示页面复制代码在这里做了一个简单的演示

import { addDays } from "date-fns";
import { useState } from "react";
import { DateRangePicker } 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

export default function IndexPage() {
  const [state, setState] = useState([
    {
      startDate: new Date(),
      endDate: addDays(new Date(), 7),
      key: "selection"
    }
  ]);

  return (
    <div>
      <h1>Hello</h1>
      <DateRangePicker
        onChange={(item) => setState([item.selection])}
        showSelectionPreview={true}
        moveRangeOnFirstSelection={false}
        months={2}
        ranges={state}
        direction="horizontal"
      />
    </div>
  );
}

Here you can see the code from my demo page. 在这里你可以从我的演示页面看到代码
Any idea or solution to stop this behavior is welcome!欢迎任何阻止这种行为的想法或解决方案! Thank you!谢谢!

This is an odd one---I'm sure someone more familiar with Next.js can update this question to flesh out exactly why this error occurs, but for now I have identified a fix and a possible explanation.这是一个奇怪的问题 --- 我相信更熟悉 Next.js 的人可以更新这个问题以明确为什么会发生这个错误,但现在我已经确定了一个修复和一个可能的解释。

I plugged your code into a create-react-app instance and it worked fine, funny enough: Sandbox here .我将您的代码插入到一个create-react-app实例中,它运行良好,很有趣: Sandbox here

I tested your same code and observed the same odd formatting.我测试了您的相同代码并观察到相同的奇怪格式。 I also noticed that the dates generated by next.js were curiously not accurate ;我还注意到next.js生成的日期奇怪地不准确 for example, it wouldn't start with today (it was off by one date, starting the selection on 'tomorrow'; this was not the case with the create-react-app version which accurately started the initial selection) so something is up with the rendering.例如,它不会从今天开始(它关闭了一个日期,在“明天”开始选择;这不是create-react-app版本的情况,它准确地开始了初始选择)所以有些事情发生了与渲染。 My best guess: next.js may not be re-rendering the <DateRangePicker /> and/or its css values (or the component in general?)---at least not when the component is initialized?---so I injected the initial data selection on load with useEffect and added a stringified version of the state as a key to the container div to force a re-render and it no longer displays the odd selection css:我最好的猜测: next.js可能不会重新渲染<DateRangePicker />和/或其 css 值(或一般的组件?)---至少在组件初始化时不会?---所以我注入了初始使用useEffect加载数据选择并添加 state 的字符串化版本作为容器 div 的键以强制重新渲染,它不再显示奇数选择 css:

import { addDays } from "date-fns";
import { useState, useEffect } from "react";
import { DateRangePicker } 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

export default function App() {
  const [state, setState] = useState([]);

  useEffect(() => {               {/* <---- useEffect to update state on init */}
    if (state.length === 0) {
      setState([
        {
          startDate: new Date(),
          endDate: addDays(new Date(), 7),
          key: "selection"
        }
      ]);
    }
  }, [state, setState]);

  return (
    <div key={JSON.stringify(state)}>     {/* <-- added key here, to force rerender */}
      <h1>Hello</h1>
      <DateRangePicker
        onChange={(item) => setState([item.selection])}
        showSelectionPreview={true}
        moveRangeOnFirstSelection={false}
        months={2}
        ranges={state}
        direction="horizontal"
      />
    </div>
  );
}

And it worked: Sandbox , demo page .它起作用了: Sandboxdemo page

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM