Skip to content

feat(react-form): add display name to components #1491

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Jul 6, 2025

Conversation

kidonng
Copy link
Contributor

@kidonng kidonng commented May 8, 2025

Currently, with the following code:

<AppForm>
  <ContainerForm>
    <AppField name="animal">
      {() => <RadioGroupField {...props} />}
    </AppField>
  </ContainerForm>
</AppForm>

Storybook's "Show code" function displays "No Display Name" for <AppForm> and <AppField>:

Screenshot 2025-05-08 at 1 17 07 PM

By assigning a displayName, they are displayed with correct names:

Screenshot 2025-05-08 at 1 16 20 PM

Assigning displayName is necessary since merely this...

    const AppForm = useMemo(() => {
-      return (({ children }) => {
+      const AppForm = (({ children }) => {

...would display as <AppForm2> instead since it is renamed when compiled:

const AppForm = useMemo(() => {
  const AppForm2 = ({ children }) => {
    return /* @__PURE__ */ jsx(formContext.Provider, { value: form, children });
  };
  AppForm2.displayName = "AppForm";
  return AppForm2;
}, [form]);

EDIT: since assigning displayName triggers a lint error, I'm resorting to simple named functions.

Copy link

nx-cloud bot commented May 8, 2025

View your CI Pipeline Execution ↗ for commit 714de59.

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 2m View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 5s View ↗

☁️ Nx Cloud last updated this comment at 2025-05-08 05:40:07 UTC

Copy link

pkg-pr-new bot commented May 8, 2025

More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@1491

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@1491

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@1491

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@1491

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@1491

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@1491

@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@1491

commit: 714de59

@kidonng
Copy link
Contributor Author

kidonng commented May 8, 2025

Well... https://github.com/TanStack/form/actions/runs/14899285839/job/41847764667?pr=1491#step:6:185

/home/workflows/workspace/packages/react-form/src/createFormHook.tsx
  278:7   error    This mutates a variable that React considers immutable                                 react-compiler/react-compiler

I am fine without the better displayName.

I also just notice more components like <Subscribe> needs this.

@kidonng kidonng marked this pull request as draft May 8, 2025 05:31
@kidonng kidonng changed the title feat(react-form): add displayName to <AppForm> & <AppField> feat(react-form): add display name to components May 8, 2025
Copy link

codecov bot commented May 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 43.94%. Comparing base (3fe66dd) to head (714de59).
Report is 53 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1491       +/-   ##
===========================================
- Coverage   88.96%   43.94%   -45.02%     
===========================================
  Files          31       13       -18     
  Lines        1396      157     -1239     
  Branches      353       26      -327     
===========================================
- Hits         1242       69     -1173     
+ Misses        137       77       -60     
+ Partials       17       11        -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kidonng kidonng marked this pull request as ready for review May 8, 2025 05:42
}, [form])

const AppField = useMemo(() => {
return (({ children, ...props }) => {
const AppField = (({ children, ...props }) => {
Copy link
Member

Choose a reason for hiding this comment

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

Does const Name = () => {} add the name to the function? For some reason I only thought that function Name() {} worked

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is easy to verify:

const Component = () => {}
Component.name // Component

Also documented: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#variable_declaration_and_method (not quite the same, but you get the idea)

@cgatian
Copy link

cgatian commented May 27, 2025

Thank you @kidonng just ran into this in Storybook docs.

@crutchcorn crutchcorn merged commit 9afa7c0 into TanStack:main Jul 6, 2025
5 of 6 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants