Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Property 'children' is missing in type '{}' but required in type '{ children: any; }

I’m attempting to learn react using a couple different sources and something isn’t working in the example.

Property ‘children’ is missing in type ‘{}’ but required in type ‘{
children: any; }

export default function Layout({ children }) {  return (
<div>
             {children}

I believe it is because I used typescript in my last example. Does anyone know how to fix this error? Do I have to know the type of children coming into this layout? If so, how do I handle this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s good to use React.FC type which automatically types your components including your props (by generic) and children props.

import React from 'react'

const Layout: React.FC = ({ children }) => {
  return (
    <div>
      {children}
    </div>
  )
}

export default Layout
import React from 'react'
import Layout from './somewhere/Layout'

const MyPageComp: React.FC = () => {
  return (
    <Layout>
      hello~ I'm children of Layout Component
    </Layout>
  )
}

export default MyPageComp
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading