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

feat(devtools middleware) add automatic action name finding #2987

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

alimertcakar
Copy link
Contributor

@alimertcakar alimertcakar commented Jan 30, 2025

Summary

image

With devtools() middleware, we can use Redux devtools extension to debug fired actions. But actions are shown as either "anonymous" or by the custom name we provided in set() function like: set(foo, undefined, "state/setCount").

It is really tedious to provide a action name for every set call. Wouldn't it be nice to have the option to have it default to a more meaningful name?

Now we can do that by passing inferActionName: true, to devtools options. It finds the the function name which called set() by reading the current stack.

This works for firefox,chrome and edge. (There isn't a redux devtools extension on Safari anyways)
In node.js it's displayed as Object.functionName instead of functionName, I don't think it matters as it's tests only.

Check List

  • pnpm run fix:format for formatting code and docs

Copy link

vercel bot commented Jan 30, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
zustand-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 2, 2025 7:37am

Copy link

codesandbox-ci bot commented Jan 30, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

pkg-pr-new bot commented Jan 30, 2025

Open in Stackblitzdemostarter

npm i https://pkg.pr.new/zustand@2987

commit: 8f12e16

)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
api.getState().setCount(10)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know how to type this, I couldn't find a similar usage in this test

Copy link
Member

Choose a reason for hiding this comment

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

It should be able to do with create<T>()(...).

@dai-shi
Copy link
Member

dai-shi commented Jan 30, 2025

This looks nice! I don't think we need inferActionName option, if this works reliably. Otherwise, I'd be a bit hesitant to merge.

@alimertcakar
Copy link
Contributor Author

This looks nice! I don't think we need inferActionName option, if this works reliably. Otherwise, I'd be a bit hesitant to merge.

Seems to work reliably on the mock project I created with a simple store.
I didn't test it on more complex projects. I can't get pnpm link --global zustand to work. Failed to resolve entry for package "zustand". The package may have incorrect main/module/exports specified in its package.json.

Is there a guide on local development? I'd like to meddle with zustand in some real life projects if possible

@dai-shi
Copy link
Member

dai-shi commented Feb 2, 2025

I'm not sure what's wrong, but you can use pkg-pr-new #2987 (comment) if you don't find a local solution.

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

Successfully merging this pull request may close these issues.

2 participants