Rya*_*iss 3 javascript reactjs gatsby
尝试将自定义道具从布局传递给子项时,我收到以下信息: 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})}
</>
)
}
Run Code Online (Sandbox Code Playgroud)
Child(也是一个功能组件)
import React from "react"
import Layout from '../components/layout'
export default () => {
return (
<Layout>
<main>
<h1>*site title will go here</h1>
</main>
</Layout>
)
}
Run Code Online (Sandbox Code Playgroud)
渲染功能模式
要使用渲染功能模式,您需要将子组件修改为
import React from "react"
import Layout from '../components/layout'
export default () => {
return (
<Layout>
{props => (<main>
<h1>{props.title}</h1>
</main>)}
</Layout>
)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7779 次 |
| 最近记录: |