Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用Gatsby createPage后如何在React组件类中使用上下文_Javascript_Node.js_Gatsby - Fatal编程技术网

Javascript 调用Gatsby createPage后如何在React组件类中使用上下文

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.

我在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.
        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>;
  }
}