如何将道具从Gatsby布局传递到子项(例如Header组件)?
import React from "react"; export default ({ children }) => { return <div>{children()}</div>; };
我发现的资源使用以下格式:
{ props.children({ ...props, foo: true }) } { this.props.children({...this.props, updateLayoutFunction }) }
但是由于没有props孩子,只有孩子,所以我不能让它工作。我试过了,但是也没用:
props
{ children({ foo: true }) }
在所有情况下,我都会收到此错误: TypeError: undefined is not an object (evaluating 'history.listen')
TypeError: undefined is not an object (evaluating 'history.listen')
https://github.com/gatsbyjs/gatsby/issues/3412#event-1446894145 https://github.com/gatsbyjs/gatsby/issues/2112
但是有道具,您只是没有将它们传递给孩子。
在您的布局文件中,而不是:
export default ({ children }) // ... etc
做
export default (props) { const myOwnProperties = { foo: true }; return ( {props.children({ ...props, ...myOwnProperties })} ); }
您会看到,道具会自动传递,直到您添加自己的道具为止。