Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid date warning in StrictMode #224

Open
veronika-viedoc opened this issue Oct 30, 2024 · 0 comments
Open

Invalid date warning in StrictMode #224

veronika-viedoc opened this issue Oct 30, 2024 · 0 comments

Comments

@veronika-viedoc
Copy link

After updating to React v.18 we started to get "Invalid date provided" warnings in the console. The warnings disappear when we remove StrictMode. We are using a custom formatDate function that uses the Intl.DateTimeFormat to format the date. It seems that during the second render the preloadedDate value is the formatted value and not the value that we provide to the component.

I've made an example here: https://codesandbox.io/p/sandbox/serverless-violet-8xnj43

image

const getFormatDate = () => {
  const o = new Intl.DateTimeFormat([], { dateStyle: "short" });

  return (date) => o.format(date);
};

export default function App() {
  const [value, setValue] = useState(new Date("2024-10-21T22:00:00.000Z"));

  const options = {
    formatDate: getFormatDate(),
  };

  return (
    <>
      <Flatpickr
        options={options}
        onChange={([date]) => setValue(date)}
        value={value}
        render={({ value }, ref) => (
          <input
            ref={ref}
            readOnly
            value={value instanceof Date ? value.toISOString() : value ?? ""}
          />
        )}
      ></Flatpickr>
    </>
  );
}

I'm not sure if there is something wrong with the way we have implemented it or if this is an actual issue. Any help is appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant