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

[DataGrid] Fail with an explicit error when API context is missing #1877

Merged
merged 3 commits into from
Jun 14, 2021

Conversation

m4theushw
Copy link
Member

Closes #1401

@m4theushw m4theushw added component: data grid This is the name of the generic UI component, not the React module! new feature New feature or request labels Jun 12, 2021
export function useGridApiContext() {
const apiRef = React.useContext(GridApiContext);

if (apiRef === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about raising a warning instead of throwing and only in dev mode to not have any bundle size impact?
Is it important enough to throw?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is important to throw as users would have issues at a later stage.

We could also consider combining it with useGridApiRef so you have a single point to get or create the apiRef 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about raising a warning instead of throwing and only in dev mode to not have any bundle size impact?

@oliviertassinari I tested with the docs and the warning wasn't sent to the console, but to the terminal that was running the dev server. A throw in this situation facilitates for the user to know where's the problem.

We could also consider combining it with useGridApiRef so you have a single point to get or create the apiRef 🤔

@dtassone Let's imagine that there's another grid inside the grid, like a child row. How will we know if a apiRef has to be created or the one in the context should used? Maybe we need to add an argument to force to create the apiRef even if there's a context in the tree.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but to the terminal that was running the dev server.

@m4theushw True, I was actually just talking about this problem in mui/material-ui#18119 (comment). It's a Next.js thing that they could solve by forwarding the console log to the client too (not only the error). But fair enough.

In the future, we could set up the error modification script that Sebastian built (to save a bit of bundle size)

How will we know if a apiRef has to be created or the one in the context should used?

💯. I would even push it one step further is unbundle useGridApiRef. I think that we need 3 hooks, for 3 different use cases, not 1.

  • useGridApiContext to consume the API ref (React.useContext)
  • const apiRef = React.useRef<GridApi>(createGridApi()); to create the apiRef and store it + React.useImperativeHandle
  • useGridApiRefto read the API ref from the child data grid ONLY (React.useRef)

I did a first step in this direction with #990

@m4theushw m4theushw merged commit 41ac011 into mui:master Jun 14, 2021
@m4theushw m4theushw deleted the useGridApiContext branch June 14, 2021 20:52
@oliviertassinari oliviertassinari changed the title [DataGrid] Add useGridApiContext to access the GridApiContext [DataGrid] Fail with an explicit error when context is missing Aug 21, 2021
@oliviertassinari oliviertassinari changed the title [DataGrid] Fail with an explicit error when context is missing [DataGrid] Fail with an explicit error when API context is missing Aug 21, 2021
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! new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Should fail with an explicit error when context is missing
4 participants