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

DataTable => Column: onCellEditComplete is fired twice #6141

Closed
Fallup opened this issue Mar 13, 2024 · 5 comments · Fixed by #6641
Closed

DataTable => Column: onCellEditComplete is fired twice #6141

Fallup opened this issue Mar 13, 2024 · 5 comments · Fixed by #6641
Assignees
Labels
React 18 Issue or pull request is *only* related to React 18 Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@Fallup
Copy link

Fallup commented Mar 13, 2024

Describe the bug

onCellEditComplete of the Column component in the DataTable is fired twice on version 10.5.1
Can be reproduced with the StackBlitz example directly from the docs.

This was working fine on version 9.6.4

Reproducer

https://stackblitz.com/edit/rb6yv7?file=src%2FApp.tsx

PrimeReact version

10.5.1

React version

18.x

Language

TypeScript

Build / Runtime

Vite

Browser(s)

No response

Steps to reproduce the behavior

Edit cell, click away and see that the onCellEditComplete is called twice.

Expected behavior

onCellEditComplete should be called only once

@Fallup Fallup 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 13, 2024
@kl-nevermore
Copy link
Contributor

looks like strict mode

@melloware melloware added React 18 Issue or pull request is *only* related to React 18 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 22, 2024
@Chimi-RE
Copy link

Chimi-RE commented May 3, 2024

Any work around before the issue gets resolved? Thank you!

@Fallup
Copy link
Author

Fallup commented May 14, 2024

Any work around before the issue gets resolved? Thank you!

The only workaround I found was to use useRef as a guard.
Set it to true/false at the start/end of the onCellEditComplete callback and add a guard at the start of the callback, e.g.:

       ...
       const cellEditCompletePending = useRef(false)
       ...
      <Column
        onCellEditComplete={async (e) => {
              if (cellEditCompletePending.current) {
                  return
              }
              cellEditCompletePending.current = true
              // do stuff
              cellEditCompletePending.current = false
        }}
        />
        ...

@didix16
Copy link
Contributor

didix16 commented May 19, 2024

I have the same issue at 10.6.5 version but only onKeyDown. I was trying to debug it and see what is causing the function to be called twice but It seems to me that is something related with React... Could it be @melloware ?

BTW, cellEditValidator is also being calld twice. In fact this code inside this function is what is being called twice when onKeyDown event is fired:

setEditingRowDataState((prev) => {
const newRowData = prev;
const newValue = resolveFieldData(newRowData);
const params = { ...callbackParams, newRowData, newValue };
const onCellEditCancel = getColumnProp('onCellEditCancel');
const cellEditValidator = getColumnProp('cellEditValidator');
const onCellEditComplete = getColumnProp('onCellEditComplete');
if (!submit && onCellEditCancel) {
onCellEditCancel(params);
}
let valid = true;
if ((!submit || cellEditValidateOnClose()) && cellEditValidator) {
valid = cellEditValidator(params);
}
if (valid) {
if (submit && onCellEditComplete) {
setTimeout(() => onCellEditComplete(params));
}
closeCell(event);
} else {
event.preventDefault();
}
return newRowData;
});

The event is only fired once but the code inside setEditingRowDataState is being called twice.

The funny thing is that only happens if the value of the cell has been changed.

Edit

@kl-nevermore has right. I just found this issue on stackoverflow: https://stackoverflow.com/questions/62106596/reactjs-setstate-being-called-twice-in-a-function-called-once-why that references this facebook/react#12856 (comment)

It seems is that is the current behaviour of React, executing twice the setter callback when using React.StrictMode.

So my question is: Is it possible that React strictMode is being activated on production release?

Maybe a solution could be execute the code inside setEditingRowDataState at outside the setter, obtaining the prev by checking the component data reference and simply just return the newRowData. I don't know.

@kl-nevermore
Copy link
Contributor

kl-nevermore commented May 20, 2024

RP submitted
use ref cached editingRowDataState

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
React 18 Issue or pull request is *only* related to React 18 Type: Bug Issue contains a defect related to a specific component.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants