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

[data grid] DataGrid gives warning about apiRef.current #15507

Closed
entaildevops opened this issue Nov 20, 2024 · 8 comments
Closed

[data grid] DataGrid gives warning about apiRef.current #15507

entaildevops opened this issue Nov 20, 2024 · 8 comments
Labels
component: data grid This is the name of the generic UI component, not the React module! customization: logic Logic customizability support: commercial Support request from paid users support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/

Comments

@entaildevops
Copy link

entaildevops commented Nov 20, 2024

The problem in depth

I am using DataGridPro in Dialog. When the dialog is opened for a second time, the following warning is printed to console:

"Warning: Failed prop type: The prop apiRef.current is marked as required in ForwardRef(DataGridPro), but its value is null."

Here is a simplified demo that demonstrates the issue. I also made a code sandbox demo.

I have a parent component for controlling when the dialog is open/closed.

import { useState } from 'react'
import { TestGrid } from './TestGrid'
import { Box, Button } from '@mui/material';

export const ParentComponent = () => {
  const [dialogOpen, setDialogOpen] = useState<boolean>(false);

  return (
    <Box>
        <Button onClick={() => setDialogOpen(true)}>Open dialog</Button>
        <TestGrid onCloseDialog={()=> setDialogOpen(false)} dialogOpen={dialogOpen}/>
    </Box>
  )
}

And a child component for the actual dialog and DataGrid:

import { Button, Dialog, DialogActions, DialogContent, DialogTitle } from '@mui/material';
import { GridColDef, useGridApiRef } from '@mui/x-data-grid-pro';
import { DataGridPro } from '@mui/x-data-grid-pro';

interface TestGridProps {
    dialogOpen: boolean;
    onCloseDialog: () => void;  
  }

export const TestGrid = (props: TestGridProps) => {
  const apiRef = useGridApiRef(); //on SECOND time the dialog is opened this is null for a brief moment and this causes a warning "Warning: Failed prop type: The prop `apiRef.current` is marked as required in `ForwardRef(DataGridPro)`, but its value is `null`."
  console.log('apiRef', apiRef)

  const columns: GridColDef[] = [
    { field: 'id', headerName: 'Id', width:10 }, 
    { field: 'name', headerName: "Name", width: 150 },        
  ];
  
  const rows = [
    {id: 1, name: "Foo"},
    {id: 2, name: "Bar"}
  ]
  const handleClose = () => {
    props.onCloseDialog();   
  }

  const handleDelete = () => {
    apiRef.current.updateRows([{ id: 1, _action: 'delete' }]);
  }

  return (
    <Dialog
        maxWidth='md'
        fullWidth                
        open={props.dialogOpen}        
        onClose={handleClose}>
        <DialogTitle>Test Grid</DialogTitle>
        <DialogContent>
        <DataGridPro 
            apiRef={apiRef}      
            columns={columns}
            rows={rows}
        />
        </DialogContent>
        <DialogActions>
            <Button onClick={handleDelete}> Delete first row</Button>
            <Button onClick={handleClose}>Close</Button>            
        </DialogActions>
    </Dialog>
  )
}

Please explain why the apiRef becomes null briefly when the dialog is reopened and causes the warning. The functionality seems fine though, but I would like to get rid of the warning (and also understand the reason for it). Thanks!

Your environment

System:
OS: Windows 11 10.0.22631
Binaries:
Node: 18.16.0 - C:\Program Files\nodejs\node.EXE
npm: 9.5.1 - C:\Program Files\nodejs\npm.CMD
pnpm: Not Found
Browsers:
Chrome: Not Found
Edge: Chromium (127.0.2651.86)
npmPackages:
@emotion/react: ^11.11.4 => 11.11.4
@emotion/styled: ^11.11.5 => 11.11.5
@mui/base: 5.0.0-beta.40
@mui/core-downloads-tracker: 5.15.20
@mui/icons-material: ^5.15.20 => 5.15.20
@mui/material: ^5.15.20 => 5.15.20
@mui/private-theming: 5.15.20
@mui/styled-engine: 5.15.14
@mui/system: 5.15.20
@mui/types: 7.2.16
@mui/utils: 5.16.6
@mui/x-data-grid: ^7.6.2 => 7.22.2
@mui/x-data-grid-pro: ^7.22.2 => 7.22.2
@mui/x-date-pickers: ^7.16.0 => 7.16.0
@mui/x-internals: 7.16.0
@mui/x-license: 7.21.0
@types/react: ^18.2.66 => 18.3.3
react: ^18.2.0 => 18.3.1
react-dom: ^18.2.0 => 18.3.1
typescript: ^5.2.2 => 5.4.5

problem is reproduced on both Edge and Chrome

Search keywords: apiRef.current
Order ID: 98745

@entaildevops entaildevops added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial Support request from paid users labels Nov 20, 2024
@github-actions github-actions bot added component: data grid This is the name of the generic UI component, not the React module! support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/ labels Nov 20, 2024
@michelengelen
Copy link
Member

Hey @entaildevops ... could you make that codesandbox public please?
I'll be glad to help afterwards! :D

@michelengelen michelengelen added status: waiting for author Issue with insufficient information customization: logic Logic customizability and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 20, 2024
@michelengelen michelengelen changed the title [question] DataGrid gives warning about apiRef.current [data grid] DataGrid gives warning about apiRef.current Nov 20, 2024
@entaildevops
Copy link
Author

@michelengelen done =)

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Nov 20, 2024
@entaildevops
Copy link
Author

any update on this @michelengelen?

@michelengelen
Copy link
Member

michelengelen commented Nov 26, 2024

any update on this @michelengelen?

not yet ... sry ... i had my hands full with other stuff, but will get to test it this week.

@michelengelen
Copy link
Member

All right ... I have looked into this and the main problem is that the children if the dialog will get unmounted and the apiRef gets "cleared" to prevent bugs due to a lingering api.

To prevent that you can use the keepMounted prop on the Dialog. Would that be a sufficient solution for you?

@michelengelen michelengelen added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 29, 2024
@entaildevops
Copy link
Author

To prevent that you can use the keepMounted prop on the Dialog. Would that be a sufficient solution for you?

Yes, looks like this is sufficient. Thanks!

Btw, it looks like this keepMounted prop is not listed in the API docs for the Dialog. This is probably because the DialogProps extend ModalProps, and for Modal the keepMounted is listed in its API docs. Nevertheless, according to the docs there is no possibility to use the keepMounted for the dialog..

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Nov 29, 2024
@michelengelen
Copy link
Member

ModalProps

I agree that we could possibly do a bit better on mentioning that. We did a similar thing for the Group extension for 'Tree Data' and 'Row grouping'.

Great that this solves your use case ... i'll close the issue! 👍🏼

Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

@entaildevops How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

@github-actions github-actions bot removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! customization: logic Logic customizability support: commercial Support request from paid users support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/
Projects
None yet
Development

No branches or pull requests

2 participants