material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

[docs] react in jsx scope

Open oliviertassinari opened this issue 1 year ago • 2 comments

Summary

SCR-20240906-ofzf

https://mui.com/material-ui/react-button/#basic-button

See the:

import * as React from 'react';

What is this for? Since React 17.0.0 https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#removing-unused-react-imports and the introduction of a new JSX transform, the import of React doesn't no longer need to be in the scope. We don't support older versions now:

https://github.com/mui/material-ui/blob/44e3ce3034de19f81aaec36c7a866cf9d45d9885/packages/mui-material/package.json#L82

So most of the demos in the docs could be written without the import of React. This would be simpler. It's actually a DX problem because of https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/react-in-jsx-scope.md.

  • Vite (latest version) warns when starting a new project and copying and pasting our demos in the docs: SCR-20240511-oeqy

  • Next.js (latest version) doesn't say anything, it's happy with having import * as React from 'react' or not having it.

Examples

They don't have those imports:

  • https://mantine.dev/core/chip/
  • https://ui.shadcn.com/docs/components/dialog

Motivation

In the past, we didn't make the change because there were too many React users <17.0.0. But this seems to be mostly OK now:

SCR-20240906-opdn

https://tools-public.mui.com/prod/pages/npmVersion?package=react

I assume that all bundlers use the new JSX transform.

Context

Related a bit to #38966.

oliviertassinari avatar May 09 '24 00:05 oliviertassinari

Agreed. We can atleast start with our own docs, update the eslint rules, and then update the examples.

brijeshb42 avatar May 10 '24 08:05 brijeshb42

Vite (latest version) warn when starting a new project and copying and pasting our demos in the docs: SCR-20240511-oeqy So there is a DX cost in having those react imports.

Next.js (latest version) doesn't say anything, it's happy with having import * as React from 'react' or not having it.

oliviertassinari avatar May 11 '24 14:05 oliviertassinari

cc @colmtuite @aarongarciah @joserodolfofreitas @samuelsycamore it looks like it's a no-brainer to deploy this change in all our demos, e.g.

-import * as React from 'react';
 import Stack from '@mui/material/Stack';
 import Button from '@mui/material/Button';

 export default function BasicButtons() {
   return (
     <Stack spacing={2} direction="row">
       <Button variant="text">Text</Button>
       <Button variant="contained">Contained</Button>
       <Button variant="outlined">Outlined</Button>
     </Stack>
   );
 }

oliviertassinari avatar Sep 06 '24 14:09 oliviertassinari