Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Issue with rendering a blockContent element nested inside an array in Gatsby

3 replies
Last updated: Apr 8, 2020
Hello, I'm trying to render a blockContent element I have inside of an array (used as an object inside a document)
But I can't figure out how to get the _raw data for the blocks properly, when a blockContent element is nested inside of an array it seems to not make it's own _raw element for Gatsby, instead it's a part of the _raw element from for the page.

Code in comments
Apr 8, 2020, 6:40 PM
My GraphQL query looks like this:const data = useStaticQuery(graphql`
    
{
      
sanityUserTesting {
        
userTestingIntro {
          
auditButton
          
mostSold
          
testTitle
          
testFeatures {
            
features
          
}
        
}
        
_rawUserTestingIntro
      
}
    
}
  ``)`


_rawUserTestingIntro
contains the elements I need, inside the
_rawUserTestingIntro
array there is an array called
_rawUserTestingIntro
I'm trying to loop over this while I loop over some other values from the query

My loop currently looks like this

return (
    
<>
      
{data.sanityUserTesting.userTestingIntro.map((node, index) => {

        
return (
          
<>

        
            
<BlockContent

              _blocks_={
                
data.sanityUserTesting._rawUserTestingIntro.auditDescirption[
                  
index
                
]
              
}

              _serializers_={defaultSerializer}
            
/>

            
{node.auditButton}
          
</>
        
)
      
})}
    `&lt;`/&gt;
  )
}
Apr 8, 2020, 6:43 PM
If I console.log
data.sanityUserTesting._rawUserTestingIntro
I get the following . So I've tried
settings_data.sanityUserTesting._rawUserTestingIntro.auditDescirption[index]_
as the block element source, but this gives me a 0 error. If Any ideas? 🙂
Apr 8, 2020, 6:46 PM
I have been struggling with this issue for so long today, but writing this questions made me solve it! I had the [index] on the wrong spot, the right way refer to the block elements was
<BlockContent

              _blocks_={
                
data.sanityUserTesting._rawUserTestingIntro[index]
                  
.auditDescirption
              
}

              _serializers_={defaultSerializer}
            
/>
Apr 8, 2020, 6:50 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?