Error using Deno Fresh NPM package and Datepicker

84 Views Asked by At

The component is created in the components folder, and then used in an island component

I have tried two ways of using the lib react-datepicker:

  • 1: Adding the import in deno.json and using it.

  • This way the datepicker just not appear on the screen

  • Vscode points shows the following error: enter image description here

  • 2: Using with npm: indicator directly onto the file

  • This way deno shows a massive error in the terminal enter image description here

My code:

import DatePicker from 'npm:[email protected]'
import {useState} from 'preact/hooks'

export function DatePickerA({name, title, required}: {name: string, title: string, required?: boolean}) {
    const [startDate, setStartDate] = useState(new Date());
    
    return (
        <label className='my-5 flex flex-col gap-y-2' for={name}>
            <span className={`text-dark-blue`}>{title}</span>
            <DatePicker selected={startDate} onChange={(date) => setStartDate(date)} />
        </label>
    )
}

How can I use react-datepicker?

1

There are 1 best solutions below

0
On

I've tried similar things, sometimes it works, other times it doesn't. Here is what I got working for react-datepicker.

This works functionally but the types will need some fixing. The main thing you need to do is update your imports in your deno.json file, making sure the preact version matches throughout:

{
  "imports": {
    ...
    "preact": "https://esm.sh/[email protected]",
    "preact/": "https://esm.sh/[email protected]/",
    ...
    "react": "https://esm.sh/[email protected]/compat",
    "react-dom": "https://esm.sh/[email protected]/compat",
    "react/jsx-runtime": "https://esm.sh/[email protected]/compat",
    "react-datepicker": "https://esm.sh/[email protected]?external=react,react-dom&target=es2022"
  }
}

Then since this uses custom CSS and deno imports don't work for CSS, we need to include that in our project manually:

_app.tsx

<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/[email protected]/dist/react-datepicker.css"
/>

You will want to wrap the react DatePicker with an HTML tag (e.g. div) or the flex-col will mess with it. I also added showIcon just to test that it works as well.

Here is what it looks like in Fresh with that all setup:

enter image description here

For reference, see these links: