Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  133] [ 7]  / answers: 1 / hits: 14417  / 5 Years ago, wed, october 30, 2019, 12:00:00

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

More From » reactjs

 Answers
3

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

[#5778] Saturday, October 26, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kayden

Total Points: 546
Total Questions: 102
Total Answers: 95

Location: Virgin Islands (U.S.)
Member since Fri, Mar 4, 2022
2 Years ago
kayden questions
;