Comments (11)
The fix has now landed in the most recent React and Next.js canaries. Thanks for the bug report!
from react.
This is, indeed, a bug — I apologize for not looking into this until now (thanks @tom-sherman for the ping on Twitter X). I remember getting the ping from @sophiebits but forgot to follow up.
Should be fixed by #27936
from react.
2024-01-11.00-42-19.mp4
`
'use client'
import { addTodo } from "@/actions/todo";
import { Todo } from "@prisma/client";
import { ElementRef, useOptimistic, useRef } from "react";
import { v4 as uuid } from 'uuid'
import AddButton from "./AddButton";
type Props = {
todos: Todo[]
}
const Form = ({ todos }: Props) => {
const formRef = useRef<ElementRef<'form'>>(null)
const [optimisticTodos, addOptimisticTodo] = useOptimistic<Todo[], string>(
todos,
(currTodos, newTodo: string) => {
return [
{
text: newTodo,
id: uuid(),
createdAt: new Date(Date.now()),
updatedAt: new Date(Date.now())
},
...currTodos
]
}
)
return(
<>
<form
ref={formRef}
action={async formData => {
formRef.current?.reset();
addOptimisticTodo(formData.get("text") as string)
await addTodo(formData)
}}
className="flex-row flex"
>
<label>Input text</label>
<input
type="text"
name="text"
className="bg-blue-950"
/>
<AddButton />
</form>
<ul className="mt-5">
{
optimisticTodos.map(todo => (
<li key={todo.id} className="mb-3">
<p>{todo.text}</p>
</li>
))
}
</ul>
</>
);
}
export default Form
`
from react.
I'm experiencing the same issue.
from react.
Me too. Downgrading to next 13 solves it for now
from react.
I'm experiencing the same issue.
from react.
same issue here.
from react.
same here, for me it sometimes flashes the correct value before changing back to the original value
from react.
Same here. It's not more experimental version, why is this happening?
from react.
same here, for me it sometimes flashes the correct value before changing back to the original value
For me it does every time
from react.
When there is no form submission in progress, the optimisticArr always returns the initial state provided to it.
I think this is an expected behaviour of useOptimistic. Or rather, the optimistic update is replaced with the new state as soon as the transition is over. When there is no async work in the transition, the optimistic update is replaced instantly.
Edit: In fact it says exactly that in the first line of the docs (emphasis my own):
useOptimistic is a React Hook that lets you show a different state while an async action is underway. It accepts some state as an argument and returns a copy of that state that can be different during the duration of an async action such as a network request.
It's expected for useOptimistic to essentially do nothing outside of async transitions.
from react.
Related Issues (20)
- Bug: eslint-plugin-react-hooks hangs on function with many conditionals
- Bug: typescript can't determine well useMemo value HOT 3
- Bug: Removal of custom element property sets it to `null` rather than `undefined` HOT 2
- [feat] I hope react supports two instructions r-if and r-for HOT 2
- Name property is not defined for option in the animation color dropdown. HOT 2
- Bug: StrictMode is causing issue when value is calculated using ref value HOT 9
- Updated the README.md file
- [DevTools Bug]: Big Memory Leak HOT 2
- Bug: hot-reload not working with express HOT 3
- Input value leak in chrome browser heap memory HOT 1
- User is unable to select multi color options at once. HOT 3
- Black CSS problem HOT 3
- Bug: <Suspense> fallback is rendered inconsistently when there’s a hydration mismatch + the boundary suspends during hydration HOT 1
- Bug: Parent rerenders unnecessarily on Child-to-Parent setter call, while child rerenders only when changes occur. HOT 6
- Bug:
- Bug:
- Bug:
- We are not able to adjust the width of the column header via keyboard. HOT 2
- cart issue HOT 1
- Bug: cart issue HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.