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

Editor: onTextChange handler does not reflect updated props #6067

Closed
artcab opened this issue Mar 1, 2024 · 6 comments · Fixed by #6102
Closed

Editor: onTextChange handler does not reflect updated props #6067

artcab opened this issue Mar 1, 2024 · 6 comments · Fixed by #6102
Labels
Type: Bug Issue contains a defect related to a specific component.

Comments

@artcab
Copy link

artcab commented Mar 1, 2024

Describe the bug

The onTextChange handler of the PrimeReact Editor component appears to be caching the initial value of props passed to its containing component, leading to inconsistencies in behavior when those props change.

Reproducer

https://stackblitz.com/edit/ylsaqj?file=src%2FApp.jsx

PrimeReact version

10.5.1

React version

18.x

Language

TypeScript

Build / Runtime

Vite

Browser(s)

No response

Steps to reproduce the behavior

  1. Attempt to access a prop value inside onTextChange handler of <Editor />.

from the stackblitz example:

  1. click on the add button a couple of times, to update the count.
  2. type in both the textarea and in the Editor.
  3. check the console: onChange can access the current value. OnTextChange reports the initial value.

Expected behavior

You should be able to access the current prop values. Instead, props return their initial value.

@artcab artcab added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Mar 1, 2024
@melloware
Copy link
Member

IS that PrimeReact version 3.3.1 correct? The latest version is 10.5.1 have you checked this bug is not already fixed?

@melloware melloware changed the title PrimeReact Editor's onTextChange handler does not reflect updated props Editor: onTextChange handler does not reflect updated props Mar 1, 2024
@artcab
Copy link
Author

artcab commented Mar 1, 2024

@melloware the version has been corrected from 3.3.1 to 10.5.1 in this issue, my bad; yes, this is happening on the latest version.

@melloware
Copy link
Member

Thanks let me take a look!

@melloware melloware added Type: Bug Issue contains a defect related to a specific component. and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Mar 1, 2024
@melloware
Copy link
Member

Weird I don't see anything obvious in the code that would cause this do you? The Editor component really doesn't have too much too it it just passes through to QuillJS?

@kl-nevermore
Copy link
Contributor

kl-nevermore commented Mar 4, 2024

This should be a problem with react Hook to define an event handler with an always-stable function identity
I simply modified code like...
Tip: code is for testing only

const fn = useEvent(()=>{
    props.onTextChange()
})

useUpdateEffect(() => {
    quill.current?.on('text-change', fn);

    return () => {
        quill.current?.off('text-change', fn);
    };
});

20240304170709_rec_
maybe also need to provide hooks caching functions like useEvent or useMemoizedFn for off event

@melloware
Copy link
Member

@kl-nevermore are you going to provide a PR? If so I can assign this one to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a defect related to a specific component.
Projects
None yet
3 participants