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

Plugins: how extend only one instance of dayjs? #655

Closed
gpbl opened this issue Aug 11, 2019 · 18 comments
Closed

Plugins: how extend only one instance of dayjs? #655

gpbl opened this issue Aug 11, 2019 · 18 comments

Comments

@gpbl
Copy link

gpbl commented Aug 11, 2019

I'm evaluating this library for react-day-picker and I have a concern about plugins.

For what I understand, extending dayjs with plugins will affect all the instances of dayjs.

E.g. if I extend dayjs with weekOfYear plugin, the dayjs instances used by an application implementing react-day-picker+dayjs will be extended too. Is it correct? If yes, how to extend just one instance of dayjs?

@iamkun
Copy link
Owner

iamkun commented Aug 11, 2019

Yes, and I can’t see if there is a problem. What’s your concern, please?

@gpbl
Copy link
Author

gpbl commented Aug 13, 2019

Thanks @iamkun for your answer,

I don't agree much with this design choice. Not that I expect it giving practical problems for now, but an external component using dayjs would alter how dayjs works in the whole app. This doesn't sound correct to me. What I would have preferred is a factory like:

const dayjs = dayjs.factory();
dayjs.extend(aPlugin); // only this instance of dayjs will use `aPlugin`

Just my feedback! Thanks for your work on dayjs!

@iamkun
Copy link
Owner

iamkun commented Aug 13, 2019

Thanks. To keep only one type of dayjs might be better I think. 😊

@iamkun
Copy link
Owner

iamkun commented Aug 26, 2019

I'll close this issue since it's been a while since it's been opened. Feel free to reopen if you have updates on this

@iamkun iamkun closed this as completed Aug 26, 2019
@szympajka
Copy link

@iamkun, I found this issue as I was actually looking for the sort of factory function.

The problem I have is this:

dayjs.extend(updateLocale);

dayjs.updateLocale('en', {
  relativeTime: {
    future: 'in %s',
    past: '%s ago',
    s: '<1 min',
    m: '1 min',
    mm: '%d mins',
    h: '1 hour',
    hh: '%d hours',
  },
});

So I updated locales according to our product needs, but now it will update it for the entire app which I believe is not what we want.

What can be a work around here?

@iamkun
Copy link
Owner

iamkun commented Oct 14, 2020

@szympajka there's no dayjs factory function atm.

In your case, it's better to use two locales.

e.g.

import somelocale from 'dayjs/locale/somelocale'

// update somelocale to other config and name
const otherlocale = {
  name: 'otherlocale',
   ...somelocale
}

// then register otherlocale
dayjs().locale(otherlocale)

// then you will have two locale config that you want

dayjs().locale(somelocale)
dayjs().locale(otherlocale)

@szympajka
Copy link

thanks, @iamkun, it helped :)

@kb-ally
Copy link

kb-ally commented Nov 10, 2021

I need to have badMutable plugin in specific places not globally , any suggestions @iamkun

@marnixhoh
Copy link

@kb-ally I have the same issue. How did you end up solving it?

@gjc9620
Copy link

gjc9620 commented Sep 16, 2022

How to change local to UTC time?
like

dayjs().locale(UTC)?

@Nantris
Copy link

Nantris commented Nov 18, 2022

This should really be re-opened. This is the same sort of unexpected behavior that people are leaving moment to get away from.

It doesn't need to be fixed per se, but closing without resolution? Why?

@adguy360
Copy link

adguy360 commented Apr 9, 2023

Hi @iamkun,

In a micro frontend architecture with a single appshell hosting multiple (full app) remotes, each remote should be able to set its own default day js configs (locale, tz ec) without affecting/overriding other remotes.
This is a common scenario with a relatively simple MF architecture, how does day js support such cases with a global singleton?

@Ecksters
Copy link

Ecksters commented May 2, 2023

Seems like you could keep the current behavior as the default, but offer a method that returns a new untouched DayJS instance, for cases like this.

@Nantris
Copy link

Nantris commented May 3, 2023

@iamkun would you be open to reconsidering your previous stance? I think many developers would prefer to use dayjs instead of moment but this remains a blocker for us, and surely for others.

@rubennaatje
Copy link

rubennaatje commented Jun 3, 2023

For me it's an issue when using it as a replacement for moment.js in a 3rd party library.

In the rest of my codebase i do want to use the non BadMutable version of Dayjs.

@switz
Copy link

switz commented Jul 17, 2023

yeah would prefer plugins/updateLocale to return an immutable instance of dayjs rather than overriding global config

@beefchimi
Copy link

@iamkun the code you shared with @szympajka does not appear to work... I am unable to have a custom locale for just one component. Example here: https://codesandbox.io/s/dayjs-testing-rr26qy

That being said, I am in agreeance with other commenters in this thread - it would be ideal to spawn a unique instance of dayjs so that we can extend it without affecting any other instance.

@Nantris
Copy link

Nantris commented Dec 12, 2023

I think this package is unmaintained, but still, friendly bump.

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