Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.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 GraphQL的组件道具_Javascript_Reactjs_Graphql_Gatsby_Netlify Cms - Fatal编程技术网

Javascript GraphQL的组件道具

Javascript GraphQL的组件道具,javascript,reactjs,graphql,gatsby,netlify-cms,Javascript,Reactjs,Graphql,Gatsby,Netlify Cms,我正在使用React和GatsbyJS构建一个简单的web应用程序,并使用NetlifyCMS管理内容。使用gatsby transformer remarkI加载包含要使用gatsby transformer sharp加载的图像路径的数据 因此,我创建了一个图像组件,它接收到道具的路径: <Image path={post.frontmatter.thumbnail} /> 做这件事的正确方法是什么?尽管我不能理解你的GraphQL类型或你的apollo??设置(我建议运行查询

我正在使用
React
GatsbyJS
构建一个简单的web应用程序,并使用
NetlifyCMS
管理内容。使用
gatsby transformer remark
I加载包含要使用
gatsby transformer sharp加载的图像路径的数据

因此,我创建了一个图像组件,它接收到道具的路径:

<Image path={post.frontmatter.thumbnail} />

做这件事的正确方法是什么?

尽管我不能理解你的GraphQL类型或你的apollo??设置(我建议运行查询,以测试如何使用graphiql或Apollo客户端开发工具获取数据,或者首先查看如何从graphql解析数据的类似工具),但您的代码应该是这样的

import React from 'react';
import Img from 'gatsby-image';
import { gql, graphql } from 'react-apollo'; //Apollo client

class Image extends React.Component {
  render() {
    return(
      <Img 
       // get access to the graphql data with this.props.data
        sizes={this.props.data.file.childImageSharp.sizes} 

      />
    );
  }
}

   const getSizesQuery = gql`
    query GatsbyImageSampleQuery($path: ID!) {
      file(relativePath: $path) {
        childImageSharp {
          sizes {
            base64
            tracedSVG
            aspectRatio
            src
            srcSet
            srcWebp
            srcSetWebp
            sizes
            originalImg
            originalName
          }
        }
      }
    }
`;

const ImageWithData =  graphql(getSizesQuery , {
  name : 'selectCustomer',
  options: (ownProps) => ({
    variables: {
      path: ownProps.path // set your path like this
    }
  })
})(Image);

export default ImageWithData 
从“React”导入React;
从“盖茨比图像”导入Img;
从'react apollo'导入{gql,graphql}//阿波罗客户机
类映像扩展了React.Component{
render(){
返回(
({
变量:{
path:ownProps.path//如下设置路径
}
})
})(图像);
导出默认图像WithData

虽然我无法确定您的GraphQL类型或apollo?设置(我建议运行查询,以测试如何使用graphiql或apollo客户端开发工具或类似工具获取数据,首先您可以看到如何从GraphQL解析数据),但您的代码应该是这样的

import React from 'react';
import Img from 'gatsby-image';
import { gql, graphql } from 'react-apollo'; //Apollo client

class Image extends React.Component {
  render() {
    return(
      <Img 
       // get access to the graphql data with this.props.data
        sizes={this.props.data.file.childImageSharp.sizes} 

      />
    );
  }
}

   const getSizesQuery = gql`
    query GatsbyImageSampleQuery($path: ID!) {
      file(relativePath: $path) {
        childImageSharp {
          sizes {
            base64
            tracedSVG
            aspectRatio
            src
            srcSet
            srcWebp
            srcSetWebp
            sizes
            originalImg
            originalName
          }
        }
      }
    }
`;

const ImageWithData =  graphql(getSizesQuery , {
  name : 'selectCustomer',
  options: (ownProps) => ({
    variables: {
      path: ownProps.path // set your path like this
    }
  })
})(Image);

export default ImageWithData 
从“React”导入React;
从“盖茨比图像”导入Img;
从'react apollo'导入{gql,graphql};//apollo客户端
类映像扩展了React.Component{
render(){
返回(
({
变量:{
path:ownProps.path//如下设置路径
}
})
})(图像);
导出默认图像WithData

盖茨比将所有数据拉入其内部节点系统,然后通过布局或页面中的查询将数据推送到组件中

这个单一的条目源确保Gatsby可以将所有数据编译为静态JSON。@toufek khoury提供的方法将数据传送到组件,但它将绕过Gatsby缓存。几乎在所有情况下,您都希望缓存数据,这就是Gatsby如此快的原因


解决方案是在
pageQuery

中获取页面所需的所有数据,盖茨比将所有数据拉入其内部节点系统,然后仅通过布局或页面中的查询将数据推送到组件中

这个单一的条目源确保Gatsby可以将所有数据编译为静态JSON。@toufek khoury提供的方法将数据传送到组件,但它将绕过Gatsby缓存。几乎在所有情况下,您都希望缓存数据,这就是Gatsby如此快的原因


解决方案是在
pageQuery

中获取页面所需的所有数据。您可以选择
contentful
cms最适合gats by framework,如果您想要contentful i have solution?您可以选择
contentful
cms最适合gats by framework,如果您想要contentful i have solution?