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

fix(core): atom toString includes debugLabel in dev mode #2659

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/vanilla/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ export function atom<Value, Args extends unknown[], Result>(
) {
const key = `atom${++keyCount}`
const config = {
toString: () => key,
toString() {
return import.meta.env?.MODE !== 'production' && this.debugLabel
? key + ':' + this.debugLabel
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious - why add a key with : here if a debugLabel is present? 🤔 Is it to avoid naming collisions?

Copy link
Collaborator Author

@dmaskasky dmaskasky Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be to make similar with useAtomsDevtools.ts#L12?

jotaijs/jotai-devtools#156 (comment)

If we are going to centralize / change logic for useAtomsDevtools.ts#L12 anyways, then does it make sense to consider a different delimiter instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arjunvegda toString must return a unique string as the use case is key={} in React. debugLabel doesn't have to be unique. So, yes, it's to avoid collisions.

: key
},
} as WritableAtom<Value, Args, Result> & { init?: Value }
if (typeof read === 'function') {
config.read = read as Read<Value, SetAtom<Args, Result>>
Expand Down