on gatsby project, i have static query in one of my components, it gets an array of objects. Then i take window width and subdivide that array on amount of chunks i can display per screen...
that's the query:
const { data } = useStaticQuery(graphql`
query AutomobilesQuery {
data: allStrapiAutomobiles(sort: { fields: created_at, order: ASC }) {
nodes {
id
slug
title
years
description {
id
}
thumbnail {
localFile {
childImageSharp {
fluid(maxWidth: 1920, quality: 100) {
...GatsbyImageSharpFluid_noBase64
}
}
}
}
}
}
}
`)
size:
const size = Math.min(4, Math.ceil(width / 454))
So the first option was
const groups = chunkArray(data.nodes, size)
After window resize event data.nodes becomes empty array....
then i did it like that:
const [groups, setGroups] = useState(chunkArray(data.nodes, size))
useEffect(() => {
setGroups(chunkArray(groups.flat(1), size)
}, [size])
now it does work after window resize event, but now if i navigate between local pages, data.nodes becomes empty array again...
The question is what can cause such behavior?
this array is around 30 items, maybe that due to size of the array? i did test with page query, but it has same behavior...
Hardly even have anything to guess... description.id is null on some items...
// UPDATE So code switch to page query
index.js
const index = ({ location, data }) => {
<Layout>
<SEO />
...
<Autos automobiles={data.automobiles}/>
...
</Layout>
)
}
export const query = graphql`
query allAutomobilesThumbsQuery {
automobiles: allStrapiAutomobiles(sort: { fields: created_at, order: ASC }) {
nodes {
id
slug
title
years
description {
id
}
thumbnail {
localFile {
childImageSharp {
fluid(maxWidth: 1920, quality: 100) {
...GatsbyImageSharpFluid_noBase64
}
}
}
}
}
}
}
`
export default index
component code
const Autos = ({automobiles}) => {
const { width } = useDisplayDetect()
const size = Math.min(4, Math.ceil(width / 454))
const [groups, setGroups] = useState(chunkArray(automobiles.nodes, size > 1 ? size * 2 : size))
useEffect(() => {
setGroups(chunkArray(groups.flat(1), size > 1 ? size * 2 : size))
}, [width])
return (
...
)
}
export default Autos
same behavior, first load everything ok, then data, automobiles.nodes empty array That's after page refresh or page load:
That's after navigation...

