mui-x icon indicating copy to clipboard operation
mui-x copied to clipboard

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

Open oliviertassinari opened this issue 2 years ago • 13 comments

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:

  • https://github.com/vercel/next.js/issues/48284
  • https://github.com/vercel/next.js/issues/53342

oliviertassinari avatar Jul 29 '23 21:07 oliviertassinari

I'm having the same problem 😭

jsleemaster avatar Aug 02 '23 02:08 jsleemaster

Bump, was just about to raise this 👍

xeofd avatar Sep 08 '23 13:09 xeofd

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 avatar Dec 29 '23 20:12 oliviertassinari

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.

oliviertassinari avatar Jan 02 '24 23:01 oliviertassinari

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

LukasTy avatar Jan 15 '24 20:01 LukasTy

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

  • make the source of the script handle configurable
  • ignore files that don't have React client-side APIs
  • allow, with JavaScript comments, to support exceptions to the logic

oliviertassinari avatar Jan 15 '24 23:01 oliviertassinari

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

cherniavskii avatar Jan 16 '24 14:01 cherniavskii

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.

oliviertassinari avatar Jan 18 '24 01:01 oliviertassinari

@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.

cherniavskii avatar Jan 18 '24 18:01 cherniavskii

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.

oliviertassinari avatar Jan 18 '24 19:01 oliviertassinari

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

MBilalShafi avatar Jan 18 '24 20:01 MBilalShafi

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.

oliviertassinari avatar Feb 22 '24 16:02 oliviertassinari

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.

carl0s-sa avatar Apr 15 '24 13:04 carl0s-sa