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: Cannot update a component while rendering a different component warning #5556

Closed
BAISTM opened this issue Dec 8, 2023 · 13 comments · Fixed by #5560, #5703, #5748 or #6641
Closed
Assignees
Labels
Type: Bug Issue contains a defect related to a specific component.
Milestone

Comments

@BAISTM
Copy link

BAISTM commented Dec 8, 2023

Describe the bug

I've got the below console error when updating a cell

image

Minimal code reproduction:

export default function PrimeDataTable() {
  const [data, setData] = React.useState([
    {
      value: '2',
    },
  ]);
  return (
    <DataTable value={data}>
      <Column
        field="value"
        editor={options => (
          <input
            value={options.value}
            onChange={event => options.editorCallback?.(event.target.value)}
          />
        )}
        onCellEditComplete={({ newValue }) => {
          setData([{ value: newValue }]);
        }}
      />
    </DataTable>
  );
}

Reproducer

https://codesandbox.io/p/sandbox/primereact-demo-forked-v94dr7?file=%2Fsrc%2FApp.js

PrimeReact version

10.1.1

React version

17.x

Language

TypeScript

Build / Runtime

Create React App (CRA)

Browser(s)

No response

Steps to reproduce the behavior

  1. Use a DataTable with cell edit mode
  2. Update a React state inside the onCellEditComplete

Expected behavior

The error Cannot update a component should not be displayed in the console.

@BAISTM BAISTM added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Dec 8, 2023
@melloware
Copy link
Member

Your reproducer works fine for me.

also you are doing it wrong from your example.

onValueChange={(e) => options.editorCallback(e.value)}

Already handles changing the data state for you and you try and do it again by doing it on the onCompleteEdit firing the hook again triggering the datatable again.

That is not how cell editing works.

@melloware melloware added Resolution: Invalid Issue or pull request is not valid in the latest version and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Dec 8, 2023
@melloware melloware changed the title ReactTable: Error when updating a cell DataTable: Error when updating a cell Dec 8, 2023
@BAISTM
Copy link
Author

BAISTM commented Dec 8, 2023

Hello @melloware,

My reproducer works functionally, but it triggers a React error in the console.

Sorry, but I don't understand what's wrong in my example.

The onChange updates the state of the input when the editing mode is enabled and the onCompleteEdit synchronize the data when the edition is validated.

How can I synchronize DataTable data without using onCompleteEdit hook ?

Thank you for your response.

@melloware
Copy link
Member

I don't have any errors in the console using your reproducer? I can edit say Quantity from 24 to 26 and it works without error?

@BAISTM
Copy link
Author

BAISTM commented Dec 8, 2023

I've got the bellow error on editing a quantity, can you retry plz? Thank you!

Warning: Cannot update a component (`CellEditingDemo`) while rendering a different component (`BodyCell`). To locate the bad setState() call inside `BodyCell`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
    at eval (https://v94dr7.csb.app/node_modules/primereact/datatable/datatable.esm.js:883:46)
    at tr
    at eval (https://v94dr7.csb.app/node_modules/primereact/datatable/datatable.esm.js:1612:31)
    at tbody
    at eval (https://v94dr7.csb.app/node_modules/primereact/datatable/datatable.esm.js:2133:34)
    at table
    at eval (https://v94dr7.csb.app/node_modules/primereact/virtualscroller/virtualscroller.esm.js:178:23)
    at div
    at div
    at eval (https://v94dr7.csb.app/node_modules/primereact/datatable/datatable.esm.js:4981:23)
    at div
    at CellEditingDemo (https://v94dr7.csb.app/src/App.js:20:55)
    at PrimeReactProvider (https://v94dr7.csb.app/node_modules/primereact/api/api.esm.js:997:26)

@BAISTM
Copy link
Author

BAISTM commented Dec 8, 2023

IMHO, this warning is due to this state update https://github.com/primefaces/primereact/blob/master/components/lib/datatable/BodyCell.js#L71C1-L73C6.

According to facebook/react#18178 (comment), a setState should be wrapped in a useEffect.

Thank you :)

@melloware melloware added Type: Bug Issue contains a defect related to a specific component. and removed Resolution: Invalid Issue or pull request is not valid in the latest version labels Dec 8, 2023
@melloware
Copy link
Member

weird yep I do see it now.

@melloware melloware self-assigned this Dec 8, 2023
@melloware melloware added this to the 10.2.2 milestone Dec 8, 2023
melloware added a commit to melloware/primereact that referenced this issue Dec 8, 2023
@melloware
Copy link
Member

OK PR Submitted. Good catch.

@BAISTM
Copy link
Author

BAISTM commented Dec 8, 2023

Thank you :)

@FAMSTM
Copy link

FAMSTM commented Jan 8, 2024

The fix in v10.3.0 doesn't solve the problem, there is still console errors when updating a cell

@melloware melloware reopened this Jan 8, 2024
@melloware melloware modified the milestones: 10.3.0, 10.3.1 Jan 8, 2024
@github-actions github-actions bot added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jan 8, 2024
@melloware
Copy link
Member

A bunch of changes accidentally got rolled back. I am adding them back in now.

@melloware melloware removed the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jan 8, 2024
melloware added a commit to melloware/primereact that referenced this issue Jan 8, 2024
@melloware melloware modified the milestones: 10.3.1, 10.4.0 Jan 9, 2024
@FAMSTM
Copy link

FAMSTM commented Jan 11, 2024

@melloware With v10.3.1, there is still the issue, this ticket should be reopened?

@melloware melloware reopened this Jan 11, 2024
@melloware melloware removed this from the 10.3.1 milestone Jan 11, 2024
@github-actions github-actions bot added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jan 11, 2024
@melloware
Copy link
Member

Weird. Let me look again because locally running in NextJS this was working without the warning error after this change.

@melloware melloware removed the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jan 11, 2024
@melloware melloware added this to the 10.4.0 milestone Jan 11, 2024
@melloware
Copy link
Member

@BAISTM I am able to reproduce it locally. Very strange but the stack trace line numbers don't really help. This is weird.

melloware added a commit to melloware/primereact that referenced this issue Jan 11, 2024
@melloware melloware changed the title DataTable: Error when updating a cell DataTable: Cannot update a component while rendering a different component warning Jan 11, 2024
melloware added a commit that referenced this issue Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment