Skip to content

Conversation

@kmiddleton14
Copy link
Contributor

  • Add Caveats section to useOptimistic page
  • Add demo to optimistically update the UI while waiting for a network request
useOptimisticDemo.mp4

@github-actions
Copy link

github-actions bot commented Dec 5, 2023

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! 🙌

@kmiddleton14 kmiddleton14 marked this pull request as ready for review December 5, 2023 18:43

#### Caveats {/*caveats*/}

* An optimistic state update needs to be called in an action or wrapped with `startTransition`. If it's used outside an action or a transition React will throw an error.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* An optimistic state update needs to be called in an action or wrapped with `startTransition`. If it's used outside an action or a transition React will throw an error.
* An optimistic state update needs to be called in an action or wrapped with `startTransition`. If it's used outside an action or a transition, React will throw an error.


---

### Optimistically update the UI while waiting for a network request {/*optimistically-update-while-waiting-for-network*/}
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm I may be forgetting something but what was the reasoning for us to add this example? It seems quite similar to the form action example above, the only difference being that it isn't in a form? I think we'd want to add some description here of why this example is worth highlighting.

In addition, I think it'd be valuable to expand the example and show a failed optimistic update.

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 it is similar but wanted to showcase another example that isn't using a form. I agree on adding in a failure example and have it as a followup

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a line about that?
Something like

Suggested change
### Optimistically update the UI while waiting for a network request {/*optimistically-update-while-waiting-for-network*/}
### Optimistically update the UI while waiting for a network request {/*optimistically-update-while-waiting-for-
network*/}
`useOptimistic` can also be used outside of forms, for any network request but within a transition.

import { startTransition, useOptimistic } from "react";

export function LikeButton({ likes, updateLikes, disabled }) {
const [optimisticLikes, addOptimisticLike] = useOptimistic(
Copy link
Contributor

Choose a reason for hiding this comment

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

Also the example for me is not updating the like count from 0->1

CleanShot 2023-12-15 at 09 45 56

re: https://react-ac6nnqh06-fbopensource.vercel.app/reference/react/useOptimistic#noun-labs-1201738-(2)

Choose a reason for hiding this comment

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

@lunaleaps One way to fix this example would be to start a transition with useTransition hook instead of using the standalone startTransition function. This way you'll be able to:

  • replace the disabled prop with the transition's pending state,
  • make the arrow function (scope) passed to startTransition async and await updateLikes inside (crucial).

It fixes the issue while slightly simplifying the code, here's the preview:
https://codesandbox.io/p/sandbox/gallant-nova-8w3s2h

@facebook-github-bot
Copy link
Collaborator

Hi @kmiddleton14!

Thank you for your pull request.

We require contributors to sign our Contributor License Agreement, and yours needs attention.

You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

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.

4 participants