GithubHelp home page GithubHelp logo

Comments (11)

acdlite avatar acdlite commented on May 2, 2024 14

The fix has now landed in the most recent React and Next.js canaries. Thanks for the bug report!

from react.

acdlite avatar acdlite commented on May 2, 2024 7

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.

JovanJevtic avatar JovanJevtic commented on May 2, 2024 2
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.

magoz avatar magoz commented on May 2, 2024 1

I'm experiencing the same issue.

from react.

Feel-ix-343 avatar Feel-ix-343 commented on May 2, 2024

Me too. Downgrading to next 13 solves it for now

from react.

rieznik avatar rieznik commented on May 2, 2024

I'm experiencing the same issue.

from react.

ipv6sq avatar ipv6sq commented on May 2, 2024

same issue here.

from react.

Elee-Lawleit avatar Elee-Lawleit commented on May 2, 2024

same here, for me it sometimes flashes the correct value before changing back to the original value

from react.

JovanJevtic avatar JovanJevtic commented on May 2, 2024

Same here. It's not more experimental version, why is this happening?

from react.

JovanJevtic avatar JovanJevtic commented on May 2, 2024

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.

tom-sherman avatar tom-sherman commented on May 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.