본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Grid v2로 업그레이드하기

원문 보기 위키 갱신

Grid v2로 업그레이드하기 (Upgrade to Grid v2)

GridLegacy 컴포넌트에서 Grid 컴포넌트로 마이그레이션하는 방법과 이유를 설명하는 가이드예요.

출처: 문서

본문

Grid 컴포넌트 버전들 (Grid component versions)

Material UI v7에서는 GridLegacy 컴포넌트가 더 이상 권장되지 않게 되고 Grid로 대체됐어요. Grid는 몇 가지 새로운 기능과 개발자 경험의 상당한 개선을 제공해요. 이 가이드는 GridLegacy에서 Grid로 업그레이드하는 방법을 설명하고, Material UI v5, v6, v7에 대한 세부 사항을 포함해요.

왜 업그레이드해야 하나요? (Why you should upgrade)

Grid는 GridLegacy에 비해 다음 개선 사항을 제공해요.

  • CSS 변수를 사용해서 클래스 선택자의 CSS 특이성을 제거해요. sx prop으로 원하는 스타일을 제어할 수 있어요.
  • 모든 그리드는 item prop을 지정하지 않아도 item으로 간주돼요.
  • offset 기능이 위치 지정에 더 많은 유연성을 줘요.
  • 중첩 그리드는 이제 깊이 제한이 없어요.
  • 구현이 음수 마진을 사용하지 않아서 넘치지 않아요.

업그레이드 방법 (How to upgrade)

사전 준비 (Prerequisites)

이 업그레이드를 진행하기 전에:

  • Material UI v5+를 사용 중이어야 해요.
  • Material UI 버전 업그레이드 중이라면, 그 업그레이드를 먼저 완료해야 해요.

1. import 업데이트

사용 중인 Material UI 버전에 따라 import를 다음과 같이 업데이트해야 해요.

 // The legacy Grid component is named GridLegacy
-import Grid from '@mui/material/GridLegacy';

 // The updated Grid component is named Grid
+import Grid from '@mui/material/Grid';

 // The legacy Grid component is named Grid
-import Grid from '@mui/material/Grid';

 // The updated Grid component is named Grid2
+import Grid from '@mui/material/Grid2';
 // The legacy Grid component is named Grid
-import Grid from '@mui/material/Grid';

 // The updated Grid component is named Unstable_Grid2
+import Grid from '@mui/material/Unstable_Grid2';

2. 레거시 props 제거

item과 zeroMinWidth prop은 업데이트된 Grid에서 제거됐어요. 안전하게 제거할 수 있어요.

-<Grid item zeroMinWidth>
+<Grid>

3. size props 업데이트

:::warning Material UI v5를 사용 중이라면 이 단계를 건너뛰세요. :::

GridLegacy 컴포넌트에서 size props는 테마의 breakpoint에 대응하는 이름이었어요. 기본 테마에서는 xs, sm, md, lg, xl이었죠.

Material UI v6부터 이 props는 업데이트된 Grid에서 size로 이름이 바뀌었어요.

 <Grid
-  xs={12}
-  sm={6}
+  size={{ xs: 12, sm: 6 }}
 >

모든 breakpoint에 대해 size가 같다면 단일 값을 사용할 수 있어요.

-<Grid xs={6}>
+<Grid size={6}>

또한 size props의 true 값은 "grow"로 이름이 바뀌었어요.

-<Grid xs>
+<Grid size="grow">

다음 codemod를 사용해 size props를 업데이트할 수 있어요.

npx @mui/codemod@latest v7.0.0/grid-props <path/to/folder>
npx @mui/codemod@latest v6.0.0/grid-v2-props <path/to/folder>
Skip this step if you're using Material UI v5.

:::warning 이 codemod는 그 전에 import를 업데이트해야 해요. :::

4. 레거시 음수 마진 옵트인 (Opt in to legacy negative margins)

:::warning Material UI v6 또는 v7을 사용 중이라면 이 단계를 건너뛰세요. :::

Material UI v5를 사용하면서 GridLegacy와 비슷한 음수 마진을 적용하고 싶다면 그리드 컨테이너에 disableEqualOverflow={true}를 지정하세요. 모든 그리드에 적용하려면 테마에 기본 props를 추가해요.

import { createTheme, ThemeProvider } from '@mui/material/styles';
import Grid from '@mui/material/Unstable_Grid2';

const theme = createTheme({
  components: {
    MuiGrid2: {
      defaultProps: {
        // all grids under this theme will apply
        // negative margin on the top and left sides.
        disableEqualOverflow: true,
      },
    },
  },
});

function Demo() {
  return (
    <ThemeProvider theme={theme}>
      <Grid container>...grids</Grid>
    </ThemeProvider>
  );
}

흔한 문제 (Common issues)

열 방향 (Column direction)

direction="column" 또는 direction="column-reverse"는 지원되지 않아요. 레이아웃이 GridLegacy와 함께 이런 값을 사용했다면 업데이트된 Grid로 전환할 때 깨질 수 있어요. 세로 레이아웃이 필요하다면 Grid 문서의 지침을 따르세요.

컨테이너 너비 (Container width)

업데이트된 Grid 컴포넌트는 기본적으로 컨테이너 전체 너비로 늘어나지 않아요. 그리드가 전체 너비로 늘어나길 원한다면 sx prop을 사용할 수 있어요.

-<GridLegacy container>
+<Grid container sx={{ width: '100%' }}>

 // alternatively, if the Grid's parent is a flex container:
-<GridLegacy container>
+<Grid container sx={{ flexGrow: 1 }}>

감싸진 Grid 컴포넌트를 codemod가 다루지 못함

제공된 codemod들은 다른 컴포넌트에 감싸이거나 styled된 Grid 컴포넌트는 다루지 못해요.

// The codemod won't cover StyledGrid
const StyledGrid = styled(Grid)({
  // styles
});

// The codemod won't cover WrappedGrid
const WrappedGrid = (props) => <Grid {...props} />;

이런 컴포넌트는 수동으로 업데이트해야 해요.

문서 페이지 (Documentation pages)

더 알아보기 (Learn more)