iamkun / iamkun/dayjs

ObjectSupport plugin issue when loading dayjs in React with script tags

Open
#2,326 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
In React, I am loading the ObjectSupport plugin in the browser in the same manner described [here](https://day.js.org/docs/en/plugin/loading-into-browser) by using script tags in the head of `public/index.html`.

Now when I import `dayjs` from `node_modules` to use the library in a component, the ObjectSupport plugin does not work correctly when creating a `dayjs` object like the way described [here](https://day.js.org/docs/en/parse/object). For example, `dayjs` takes this input `dayjs({ year: 2023, month: 7, day: 6, })` and outputs

```
{
$D: NaN
$H: NaN
$L: "en"
$M: NaN
$W: NaN
$d: Invalid Date {}
$m: NaN
$ms: NaN
$s: NaN
$u: undefined
$x: {}
$y: NaN
}
```
However when I remove the import statement `import dayjs from 'dayjs'`, the library and plugin load correctly and output the correct result, albeit a `'dayjs' is not defined` ESLint error is shown when the component is rendered. I could silence the error with `// eslint-disable-next-line no-undef`, but I don't understand why this is happening in the first place.

Am I loading the plugin incorrectly in a React app?

**Expected behavior**
The plugin works correctly when it is imported in head of the HTML file as described [here,](https://day.js.org/docs/en/plugin/loading-into-browser) and also when `dayjs` is imported from `node_modules`.

**Information**
- dayjs: ^1.11.8
- react: ^18.2.0
- react-dom: ^18.2.0
- react-router-dom: ^6.10.0

Contributor guide

Open the contributing guide

Research direction

Start with the script-tag loading example linked in the issue and compare it with the React app's public/index.html and the component's `import dayjs from 'dayjs'`. Reproduce the ObjectSupport call with the reported package versions, then determine whether both loading paths can work together. Done means the object input produces a valid Day.js value when the plugin is loaded from the HTML and Day.js is imported in the component.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.