admin管理员组文章数量:1356764
While developing my personal portfolio using Next.js, I ran into an error where the prop "children" was not working. I am using tsx.
Layout.tsx
import styles from "./layout.module.css";
export default function Layout({ children, ...props }) {
return (
<>
<main className={styles.main} {...props}>{children}</main>
</>
);
}
I tried using other props, defining the props, like I was from tutorials online, but none of them worked. ;(
While developing my personal portfolio using Next.js, I ran into an error where the prop "children" was not working. I am using tsx.
Layout.tsx
import styles from "./layout.module.css";
export default function Layout({ children, ...props }) {
return (
<>
<main className={styles.main} {...props}>{children}</main>
</>
);
}
I tried using other props, defining the props, like I was from tutorials online, but none of them worked. ;(
Share Improve this question asked Dec 3, 2022 at 19:56 David GuriDavid Guri 1631 silver badge10 bronze badges 1-
Those fragments (
<>
) are not necessary. You can learn more here: reactjs/docs/fragments.html – ddblair Commented Dec 3, 2022 at 20:10
2 Answers
Reset to default 8Your filetype is .tsx, which is a TypeScript filetype. You have to either define the types of the props, or change the file's name to Layout.js to convert it to vanilla JavaScript.
If you want to use TypeScript:
import { ReactNode } from "react";
import styles from "./layout.module.css";
const type LayoutProps = {
children: ReactNode;
// Your other props here.
}
export default function Layout({ children, ...props }: LayoutProps) {
return (
<>
<main className={styles.main} {...props}>{children}</main>
</>
);
}
The issue is that when TypeScript doesn't know the type
of something, it will assume the type is any
and plain. You could explicitly set the type as any
to solve this problem, as well, but this is considered bad practice:
export default function Layout({ children, ...props }: any) {
return (
<>
<main className={styles.main} {...props}>{children}</main>
</>
);
}
One way to define a layout.tsx file in typescript with Next.js 14+ we can import the LayoutProps:
import { LayoutProps } from "@/.next/types/app/page";
import styles from "./layout.module.css";
export default function Layout({ children, ...props }: LayoutProps) {
return (
<>
<main className={styles.main} {...props}>{children}</main>
</>
); }
版权声明:本文标题:javascript - Binding element 'children' implicitly has an 'any' type.ts(7031) in Next.js - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743994793a2572791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论