-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Add network usage example and caveat to useOptimistic page #6463
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -59,6 +59,11 @@ function AppContainer() { | |||||||||||
| * `optimisticState`: The resulting optimistic state. It is equal to `state` unless an action is pending, in which case it is equal to the value returned by `updateFn`. | ||||||||||||
| * `addOptimistic`: `addOptimistic` is the dispatching function to call when you have an optimistic update. It takes one argument, `optimisticValue`, of any type and will call the `updateFn` with `state` and `optimisticValue`. | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| #### 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. | ||||||||||||
|
|
||||||||||||
| --- | ||||||||||||
|
|
||||||||||||
| ## Usage {/*usage*/} | ||||||||||||
|
|
@@ -143,3 +148,67 @@ export async function deliverMessage(message) { | |||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| </Sandpack> | ||||||||||||
|
|
||||||||||||
| --- | ||||||||||||
|
|
||||||||||||
| ### Optimistically update the UI while waiting for a network request {/*optimistically-update-while-waiting-for-network*/} | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a line about that?
Suggested change
|
||||||||||||
|
|
||||||||||||
| <Sandpack> | ||||||||||||
|
|
||||||||||||
| ```js like-button.js active | ||||||||||||
| import { startTransition, useOptimistic } from "react"; | ||||||||||||
|
|
||||||||||||
| export function LikeButton({ likes, updateLikes, disabled }) { | ||||||||||||
| const [optimisticLikes, addOptimisticLike] = useOptimistic( | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 re: https://react-ac6nnqh06-fbopensource.vercel.app/reference/react/useOptimistic#noun-labs-1201738-(2) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
It fixes the issue while slightly simplifying the code, here's the preview: |
||||||||||||
| likes, | ||||||||||||
| (likes) => likes + 1 | ||||||||||||
| ); | ||||||||||||
|
|
||||||||||||
| async function addLike() { | ||||||||||||
| startTransition(() => { | ||||||||||||
| addOptimisticLike(); | ||||||||||||
| updateLikes(likes); | ||||||||||||
| }) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return ( | ||||||||||||
| <button disabled={disabled} onClick={addLike}> | ||||||||||||
| Like {optimisticLikes} | ||||||||||||
| </button> | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| ```js App.js | ||||||||||||
| import { useState } from "react"; | ||||||||||||
| import { LikeButton } from "./like-button.js"; | ||||||||||||
|
|
||||||||||||
| export default function App() { | ||||||||||||
| const [likes, setLikes] = useState(0); | ||||||||||||
| const [updating, setUpdating] = useState(false); | ||||||||||||
| async function updateLikes(num) { | ||||||||||||
| setUpdating(true); | ||||||||||||
| await new Promise((res) => setTimeout(res, 1000)); | ||||||||||||
| setLikes(num + 1); | ||||||||||||
| setUpdating(false); | ||||||||||||
| } | ||||||||||||
| return ( | ||||||||||||
| <LikeButton likes={likes} updateLikes={updateLikes} disabled={updating} /> | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ```json package.json hidden | ||||||||||||
| { | ||||||||||||
| "dependencies": { | ||||||||||||
| "react": "experimental", | ||||||||||||
| "react-dom": "experimental", | ||||||||||||
| "react-scripts": "^5.0.0" | ||||||||||||
| }, | ||||||||||||
| "main": "/index.js", | ||||||||||||
| "devDependencies": {} | ||||||||||||
| } | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| </Sandpack> | ||||||||||||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.