reactjs - MUI v5 : Failed prop type: The prop `direction` of `Grid` can only be used together with the `container` prop

标签 reactjs material-ui styled-components

我正在设计 MUI v5 Grid 样式,如下所示:

const SettingsGrid = styled(Grid)(({ theme }) => ({
    width: `calc(100% - calc(${Number(theme.spacing(2))} * 2))`,
    margin: theme.spacing(2),

    '& .MuiCard-root + .MuiTypography-subtitle1': {
        marginTop: theme.spacing(4),
    },
}))

然后,我按如下方式使用它:

<Box m={1} overflow="auto">
  <SettingsGrid
      container
      direction="row"
      justifyContent="center"
  >
      <Grid
          direction="column"
          style={{ minWidth: '35em', maxWidth: '60em' }}
      >
          <!-- ... -->
      </Grid>
    </SettingsGrid>
</Box>

现在,这会在运行时引发此控制台警告:

Failed prop type: The prop `direction` of `Grid` can only be used together with the `container` prop.

我尝试过使用 container={true} 但这没有帮助。似乎 container 属性在 styled() 中丢失了。我怎样才能解决这个问题?我很茫然。

伊迪丝:https://codesandbox.io/s/gritty-styled-grid-7l04g

最佳答案

只有当 container 属性设置为 true 时,Griddirection 属性才有效,因此在内部 Grid 中添加缺少的 container 属性。在内部,容器 Grid 将显示设置为 flex,并且 direction 映射到 flex-direction,如果布局不是 Flex,flex-direction 就没有意义:

<Grid container direction='column'

关于reactjs - MUI v5 : Failed prop type: The prop `direction` of `Grid` can only be used together with the `container` prop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69543461/

相关文章:

javascript - 在 React 中根据高度显示更多内容

reactjs - 是的 - 如何有条件地验证至少设置了 n 个值之一?

reactjs - 无法在 react 中正确显示路线

javascript - 为什么 jest 正在寻找一个不再存在的测试文件?

reactjs - React Material UI 条件渲染表单元格颜色

reactjs - 强制 TextField 在焦点上选择全文

javascript - 如何打印React虚拟网格?

javascript - 如何在单击按钮时更新 react 上下文提供程序状态

reactjs - 带有 animate.css 的样式组件

reactjs - 带有样式组件的 TypeScript