简体   繁体   中英

Receiving props.children is not a function

When attempting to pass custom props from layout to children, I am receiving the following: TypeError: props.children is not a function

Layout (functional component summary)

import React, { useState } from 'react'
import { useStaticQuery, graphql } from 'gatsby'

export default (props) => {
    const {site} = useStaticQuery(
        graphql`
            {
                site {
                    siteMetadata {
                        title
                    }
                }
            }
        `
    )
    const globals = {title: site.siteMetadata.title}

    return (
        <>
            {props.children({...props, ...globals})}
        </>
    )
}

Child (also a functional component)

import React from "react"
import Layout from '../components/layout'

export default () => {
    return (
        <Layout>
            <main>
                <h1>*site title will go here</h1>
            </main>
        </Layout>
    )
}

Render function Pattern

To use render function pattern you need to modified your child component as

import React from "react"
import Layout from '../components/layout'

export default () => {
    return (
        <Layout>
            {props => (<main>
                <h1>{props.title}</h1>
            </main>)}
        </Layout>
    )
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM