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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][Box] Box throws an error when there is a RSC (React Server Component) as a child #39928

Open
mr-rpl opened this issue Nov 19, 2023 · 8 comments
Assignees
Labels
component: Box The React component. external dependency Blocked by external dependency, we can鈥檛 do anything about it nextjs package: material-ui Specific to @mui/material

Comments

@mr-rpl
Copy link

mr-rpl commented Nov 19, 2023

Steps to reproduce 馃暪

Link to live example: https://codesandbox.io/p/devbox/fragrant-forest-gn5rr6?file=%2Fsrc%2Fapp%2Fpage.tsx%3A1%2C1

import { Box } from '@mui/material'

async function FooBar() {
  return <Box>foobar</Box>
}

export default function Page() {
  return (
    <Box>
      <FooBar />
    </Box>
  )
}

Current behavior 馃槸

Throws the following error:

app-index.js:32 Warning: Failed prop type: Invalid prop children supplied to ForwardRef(Box), expected a ReactNode.

Expected behavior 馃

Not error =D

Context 馃敠

Absolutely love MUI, but this is preventing us from using it in server components

Your environment 馃寧

System:
OS: macOS 13.6
Binaries:
Node: 18.17.0 - ~/.nodenv/versions/18.17.0/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 9.6.7 - ~/.nodenv/versions/18.17.0/bin/npm
Browsers:
Chrome: 119.0.6045.159
Edge: Not Found
Safari: 16.6

@mr-rpl mr-rpl added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Nov 19, 2023
@sadik-malik
Copy link
Contributor

Getting the prop type warning for Box component. https://codesandbox.io/p/sandbox/fragrant-forest-gn5rr6?file=%2Fsrc%2Fapp%2Fpage.tsx%3A1%2C1

 Warning: Failed prop type: Invalid prop `children` supplied to `ForwardRef(Box)`, expected a ReactNode.

@mj12albert mj12albert changed the title Box throws an error when there is a RSC (React Server Component) as a child [Box][material-ui] Box throws an error when there is a RSC (React Server Component) as a child Nov 20, 2023
@zannager zannager added the component: Box The React component. label Nov 20, 2023
@mj12albert mj12albert added package: material-ui Specific to @mui/material nextjs and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Nov 21, 2023
@mj12albert
Copy link
Member

This is the PropType that's causing the error: https://github.com/mui/material-ui/blob/master/packages/mui-material/src/Box/Box.js#L26

I guess we could change it to oneOfType but I'm not sure what it should be 馃

I believe this doesn't work in "plain" react, only in Next.js:

async function FooBar() {
  return <Box>foobar</Box>
}

export default function Page() {
  return (
    <Box>
      <FooBar />
    </Box>
  )
}

@sadik-malik
Copy link
Contributor

@mj12albert I was wondering if prop-types package supports the pattern of using RSC as a child.

@gentlementlegen
Copy link

gentlementlegen commented Nov 22, 2023

Can confirm it happens, and not only with Box but other components too. However there don't seem to be any issue, beyond the error in the console itself, everything behaves as expected.
If you wrap the server component within a regular div such as:

<Box>
    <div>
        <MyServerComponent />
    </div>
</Box>

the error goes away. Not ideal because of the useless extra div, but works fine so far.

@naoki-kiryu-lvgs
Copy link

naoki-kiryu-lvgs commented Nov 29, 2023

I found another solution based on @FernandVEYRIER one, without extra div element.

  1. Create an wrapper component
'use client';

import React from 'react';

export const ClientToServer: React.FC<{
  children: React.ReactNode;
}> = ({ children }) => {
  return <>{children}</>;
};
  1. Just wrap your server component with a wrapper
<Box>
    <ClientToServer>
        <MyServerComponent />
    </ClientToServer>
</Box>

@siriwatknp
Copy link
Member

This is the PropType that's causing the error: https://github.com/mui/material-ui/blob/master/packages/mui-material/src/Box/Box.js#L26

I guess we could change it to oneOfType but I'm not sure what it should be 馃

I believe this doesn't work in "plain" react, only in Next.js:

async function FooBar() {
  return <Box>foobar</Box>
}

export default function Page() {
  return (
    <Box>
      <FooBar />
    </Box>
  )
}

How about changing it to any?

@DiegoAndai DiegoAndai changed the title [Box][material-ui] Box throws an error when there is a RSC (React Server Component) as a child [material-ui][Box] Box throws an error when there is a RSC (React Server Component) as a child Dec 1, 2023
@siriwatknp
Copy link
Member

Please see facebook/prop-types#407

@siriwatknp siriwatknp added the external dependency Blocked by external dependency, we can鈥檛 do anything about it label Feb 13, 2024
@oliviertassinari
Copy link
Member

oliviertassinari commented Feb 14, 2024

We could:

  • report the bug to Next.js. It seems to be the root problem. They didn't correctly udpate react-is
  • skip the proptypes generation for they children prop, until Next.js fix it.

@siriwatknp siriwatknp added ready to take Help wanted. Guidance available. There is a high chance the change will be accepted and removed ready to take Help wanted. Guidance available. There is a high chance the change will be accepted labels Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: Box The React component. external dependency Blocked by external dependency, we can鈥檛 do anything about it nextjs package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants