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

Remove useMemo from useFormStatus example #6658

Merged
merged 2 commits into from
Feb 24, 2024

Conversation

rickhanlonii
Copy link
Member

@rickhanlonii rickhanlonii commented Feb 23, 2024

Overview

The useMemo here breaks the rules of React, is unnecessary and makes a worse experience:

  • Breaks the rules of React: setting a timer in memo, setting state, and reading/writing to refs are all side effects in render, which is unsafe. useMemo must be a pure function with no side effects.
  • Unnecessary: the formData is automatically updated as state, so there's no need to store it in a ref ref, you can just read it. The showSubmitted state is also unnecessary since the pending state already tells you if it's being submitted.
  • Worse experience: by using a timer, there is an extra state update to show the submitted state (causing a multi-pass render update), and there is an artificial delay between when the form submission completes and when the "Submitted request" text dismisses. By using the pending and formData values directly, the "Submitted request` text is tied directly to the form submission transition "pending" state, so the disabled state and the text update in the same commit.

Additional improvements

I also made a couple more improvements:

  • CSS to space things out
  • Reset the form after submission
  • Increase form submission time to 2 seconds
  • Disable the input on submit

Before

Screen.Recording.2024-02-23.at.11.50.59.AM.mov

After

Screen.Recording.2024-02-23.at.11.51.44.AM.mov

Copy link

vercel bot commented Feb 23, 2024

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

Name Status Preview Updated (UTC)
react-dev ✅ Ready (Inspect) Visit Preview Feb 24, 2024 3:33pm

Copy link

github-actions bot commented Feb 23, 2024

Size changes

📦 Next.js Bundle Analysis for react-dev

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

@lunaleaps
Copy link
Contributor

I think I added this example and I don't recall why I didn't disable the input while submitting -- is there a reason why we changed that?

Copy link
Member

@sophiebits sophiebits left a comment

Choose a reason for hiding this comment

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

ty

src/content/reference/react-dom/hooks/useFormStatus.md Outdated Show resolved Hide resolved
) : null}
</>
<br />
<p>{pending ? `Requesting ${data?.get("username")}...`: ''}</p>
Copy link
Member

Choose a reason for hiding this comment

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

Nit

Suggested change
<p>{pending ? `Requesting ${data?.get("username")}...`: ''}</p>
<p>{data ? `Requesting ${data.get("username")}...`: ''}</p>

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense - out of curiosity, are pending and data always in sync?

@rickhanlonii
Copy link
Member Author

@lunaleaps if you don't disable the input, then the user can continue typing but when the action finishes the reset will blow away their changes, which is sucky

Screen.Recording.2024-02-24.at.10.34.28.AM.mov

@rickhanlonii rickhanlonii merged commit a853e69 into reactjs:main Feb 24, 2024
4 checks passed
@rickhanlonii rickhanlonii deleted the rh/fix-memo branch February 24, 2024 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants