mui / mui-x

MUI X: Build complex and data-rich applications using a growing list of advanced React components, like the Data Grid, Date and Time Pickers, Charts, and more!
https://mui.com/x/
4.55k stars 1.33k forks source link

Add `use client` directive to all components in a new build step #9833

Open oliviertassinari opened 1 year ago

oliviertassinari commented 1 year ago

Summary 💡

Same as https://github.com/mui/material-ui/issues/37503

Examples 🌈

No response

Motivation 🔦

To feel the pain:

  1. Create a new project with the example linked on https://mui.com/material-ui/guides/next-js-app-router/
  2. Copy and paste this example https://mui.com/x/react-charts/lines/#data-format
  3. ❌ Crash
Screenshot 2023-07-29 at 23 27 36
  1. ✅ Add "use client" in your file

Saving step 4. would be great.

Context

This is part of improving the integration with Next.js App Router. I have also noticed bugs with it, but these are beyond our control:

jsleemaster commented 1 year ago

I'm having the same problem 😭

xeofd commented 1 year ago

Bump, was just about to raise this 👍

oliviertassinari commented 11 months ago

The components in the lab are getting covered https://github.com/mui/material-ui/pull/40358.

Does someone has a practical use case for this issue? I guess with rich components like MUI X has its rare for the component that renders it to not already have "use client"? So maybe the value is on the ease of getting started?

oliviertassinari commented 10 months ago

Oh actually, I just hit the case playing with the component, with the code copied from the docs:

Screenshot 2024-01-03 at 00 22 45

Not a major annoyance.

LukasTy commented 10 months ago

Clarifying that MUI Core uses https://github.com/mui/material-ui/blob/master/packages/rsc-builder/buildRsc.ts to automate it.

oliviertassinari commented 10 months ago

Which is a script that I think we can make much better:

cherniavskii commented 10 months ago

It seems like for the Data Grid packages, we can add 'use client'; to the index files since everything is imported from there: https://github.com/mui/mui-x/blob/next/packages/grid/x-data-grid/src/index.ts

oliviertassinari commented 10 months ago

The closer to the use of client APIs, the better though. If people want to use some of the API on the server, a use client in all the index would prevent them to.

Maybe one in https://github.com/mui/mui-x/blob/next/packages/grid/x-data-grid/src/DataGrid/DataGrid.tsx would cover 80% of the problem.

cherniavskii commented 10 months ago

@oliviertassinari Overall, I agree. However, I can't find a single component exported from the data grid packages that could be used as server components - all of them use hooks.

oliviertassinari commented 10 months ago

However, I can't find a single component exported from the data grid packages that could be used as server components - all of them use hooks.

@cherniavskii I wonder about the work we are doing to better support server-side integration though. We experienced cases like this in the core: https://github.com/mui/material-ui/issues/42750.

MBilalShafi commented 10 months ago

Interesting point, though as of now I can not foresee any server-rendered component for the server-side integration as the initial goal is data fetching on the client, I'm not sure if that'll be the case later on too. Let's start with adding "use client" in DataGrid.tsx

oliviertassinari commented 9 months ago

Oh, now I might understand why nobody asked for using a data grid from a server component, but asked for the charts.

It might be because of https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#passing-props-from-server-to-client-components-serialization, the columns definitions of the data grid, and as many other props aren't serializable, so people need to add "use client" where they render them <DataGrid>.

So the benefits of adding "use client" on MUI X Data Grid is very limited.

For example, I don't understand why they do this https://www.telerik.com/kendo-react-ui/components/server-components/, for charts yes mui/material-ui#12180, but for a data grid I don't see the point.

carl0s-sa commented 7 months ago

Hey! Are there any plans of adding "use client" for LocalizationProvider and other components so we can use this in next 14? I'm using mui/x-date-pickers-pro and it's not letting me place the provider in the layout without exporting it from an "in-between" client file.