Javascript 调用Gatsby createPage后如何在React组件类中使用上下文
我在gatsby-node.js文件中调用了这个函数Javascript 调用Gatsby createPage后如何在React组件类中使用上下文,javascript,node.js,gatsby,Javascript,Node.js,Gatsby,我在gatsby-node.js文件中调用了这个函数 createPage({ path: `/article/`, component: path.resolve(`./src/pages/article.js`), // The context is passed as props to the component as well // as into the component's GraphQL query.
createPage({
path: `/article/`,
component: path.resolve(`./src/pages/article.js`),
// The context is passed as props to the component as well
// as into the component's GraphQL query.
context: {
craftercmsData
},
})
如何在InExpage类中访问上下文?
上下文作为道具(作为pageContext
)提供,因此您可以轻松地使用它,而不是使用功能组件或基于类的组件来访问它,但它将仅在/src/pages/article.js
上可用:
function IndexPage(props) {
console.log("your context is", props.pageContext);
return <h1>Hello</h1>;
}
函数索引扩展(道具){
log(“您的上下文是”,props.pageContext);
回复你好;
}
或者,遵循基于类的组件:
类IndexPage扩展了React.Component{
class IndexPage extends React.Component {
console.log("your context is", this.props.pageContext);
render() {
return <h1>Hello</h1>;
}
}
class IndexPage扩展了React.Component{
log(“您的上下文是”,this.props.pageContext);
render(){
回复你好;
}
}
有用资源:
class IndexPage extends React.Component {
console.log("your context is", this.props.pageContext);
render() {
return <h1>Hello</h1>;
}
}