reactjs - 文档中的反应示例显示重载错误
问题描述
我们刚刚在 React 中启动了一个项目,我对它很陌生。简而言之,我们希望以响应式的方式将组件放置在我们的网页中。所以我只是在 reacts 文档中尝试以下示例,它不断抛出以下错误,老实说我无法理解:
No overload matches this call.
Overload 1 of 2, '(props: { component: ElementType<any>; } & { alignContent?: GridContentAlignment | undefined; alignItems?: GridItemsAlignment | undefined; ... 13 more ...; zeroMinWidth?: boolean | undefined; } & CommonProps<...> & Pick<...>): Element', gave the following error.
Type '{ xs: number; md: number; }' is not assignable to type 'GridSpacing | undefined'.
Type '{ xs: number; md: number; }' is not assignable to type '9'.
Overload 2 of 2, '(props: DefaultComponentProps<GridTypeMap<{}, "div">>): Element', gave the following error.
Type '{ xs: number; md: number; }' is not assignable to type 'GridSpacing | undefined'.
Type '{ xs: number; md: number; }' is not assignable to type '9'.ts(2769)
Grid.d.ts(141, 5): The expected type comes from property 'spacing' which is declared here on type 'IntrinsicAttributes & { component: ElementType<any>; } & { alignContent?: GridContentAlignment | undefined; alignItems?: GridItemsAlignment | undefined; ... 13 more ...; zeroMinWidth?: boolean | undefined; } & CommonProps<...> & Pick<...>'
Grid.d.ts(141, 5): The expected type comes from property 'spacing' which is declared here on type 'IntrinsicAttributes & { alignContent?: GridContentAlignment | undefined; alignItems?: GridItemsAlignment | undefined; ... 13 more ...; zeroMinWidth?: boolean | undefined; } & CommonProps<...> & Pick<...>'
(JSX attribute) spacing?: GridSpacing | undefined
Defines the space between the type item component. It can only be used on a type container component.
显然,导致错误的代码块是这样的
spacing={{ xs: 2, md: 3 }}
我知道这对专家来说可能很简单,但正如我提到的,我刚刚开始学习 React。解决方案是什么,所以我应用该示例而没有任何错误?
编辑:添加了完整的代码
import { Box, Grid, Paper, styled } from '@material-ui/core';
const Item = styled(Paper)(({ theme }) => ({
...theme.typography.body2,
padding: theme.spacing(2),
textAlign: 'center',
color: theme.palette.text.secondary,
}));
function App() {
return (
<Box sx={{ flexGrow: 1 }}>
<Grid container spacing={{ xs: 2, md: 3 }} columns={{ xs: 4, sm: 8, md: 12 }}>
{Array.from(Array(6)).map((_, index) => (
<Grid item xs={2} sm={4} md={4} key={index}>
<Item>xs=2</Item>
</Grid>
))}
</Grid>
</Box>
);
}
export default App;
解决方案
我猜你不是在容器元素上使用它。最好粘贴代码以验证这一点。只需在错误消息的最后一行记下。
定义类型项组件之间的空间。它只能用于类型容器组件。
推荐阅读
- python - 如何在 inspect.getmembers 中跳过缓存的属性
- qt - QDoubleSpinBox 在 Qt 5 中带有鼠标滚动信号?
- java - 2D Array,对 Null 元素使用 get 方法,有没有优雅的解决方案?
- tensorflow - 《Recurrent Neural Networks for Drawing Classification》教程中如何导出保存的模型
- regex - 怎么 ?在python的正则表达式中使用?
- python - 如果两个 Tree 具有相同的值节点,则使用 set 计数
- laravel - 查询与 slug 而不是 id 的 hasMany 关系?
- nginx - 如何配置 nginx 反向代理
- batch-file - 如何运行批处理脚本以获取文本文件中的输出
- r - 如何使用 plotweb 二分中的 ifelse() 语句根据交互值更改交互的颜色?