接收props.children不是函数

时间:2019-10-30 06:25:07

标签: javascript reactjs gatsby

当尝试将自定义道具从布局传递给孩子时,我收到以下消息:<Project Sdk="Microsoft.NET.Sdk" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

布局(功能组件摘要)

TypeError: props.children is not a function

孩子(也是功能组件)

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})}
        </>
    )
}

1 个答案:

答案 0 :(得分:2)

渲染功能模式

要使用渲染功能模式,您需要将子组件修改为

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

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