gpt4 book ai didi

reactjs - 在《 Gatsby 》中将 Prop 从布局传递给 child

转载 作者:行者123 更新时间:2023-12-03 13:44:11 25 4
gpt4 key购买 nike

如何将 props 从 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,只有 child ,所以我无法让它工作。我尝试了这个,但它也不起作用:

{ children({ foo: true }) }

在所有情况下,我都会收到此错误: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

最佳答案

但是 Prop 是有的,只是你没有把它们传给 child 而已。

在布局文件中,而不是:

export default ({ children }) // ... etc

export default (props) {
const myOwnProperties = { foo: true };

return (
{props.children({ ...props, ...myOwnProperties })}
);
}

你看, Prop 会自动传递,直到您添加自己的 Prop 。

关于reactjs - 在《 Gatsby 》中将 Prop 从布局传递给 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48725557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com