admin管理员组文章数量:1418095
I am using Apollo Client with Nextjs and would like to deconstruct values for easier reading.
I am trying to do this insisde pages/index.js
import { useQuery } from '~/lib/apollo'
return {
const { data: { allAwards = []} = {} } = useQuery(allAwards) //ReferenceError: Cannot access 'allAwards' before initialization
.....
}
lib/apollo
export const useQuery = function(query) {
const { enqueueSnackbar } = useSnackbar()
const { options = {} } = sortParams([...arguments])
const { loading, data: queryData, error, refetch } = HookQuery(query, {
fetchPolicy: 'cache-and-network',
...options,
})
let transformData = {}
if (queryData) transformData = new ApolloClass(queryData).start()
if (error && !options.noError) hookLogger(enqueueSnackbar, error)
return {
queryData,
error,
loading,
data: transformData,
refetch,
}
}
data
{
data: {
allAwards: []
}
}
I am using Apollo Client with Nextjs and would like to deconstruct values for easier reading.
I am trying to do this insisde pages/index.js
import { useQuery } from '~/lib/apollo'
return {
const { data: { allAwards = []} = {} } = useQuery(allAwards) //ReferenceError: Cannot access 'allAwards' before initialization
.....
}
lib/apollo
export const useQuery = function(query) {
const { enqueueSnackbar } = useSnackbar()
const { options = {} } = sortParams([...arguments])
const { loading, data: queryData, error, refetch } = HookQuery(query, {
fetchPolicy: 'cache-and-network',
...options,
})
let transformData = {}
if (queryData) transformData = new ApolloClass(queryData).start()
if (error && !options.noError) hookLogger(enqueueSnackbar, error)
return {
queryData,
error,
loading,
data: transformData,
refetch,
}
}
data
{
data: {
allAwards: []
}
}
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Dec 28, 2019 at 22:50
Jamie HutberJamie Hutber
28.1k54 gold badges194 silver badges313 bronze badges
1 Answer
Reset to default 3You're using destructuring assignment to declare a variable named allAwards
from the value returned by the useQuery
hook -- and then you try to pass that same variable to the hook. As the error indicates, you can't use a variable before its declared.
const { data: { allAwards = []} = {} } = useQuery(allAwards)
^ this ^ same as this
There's no reason to use a query result as the first parameter in useQuery
in the first place -- this value should always be a DocumentNode object (which is what's returned when you use the gql
tagged template). Maybe you have a naming conflict in there somewhere?
本文标签:
版权声明:本文标题:javascript - Deconstructing values nextjs: ReferenceError: Cannot access 'XXX' before initialization - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745277916a2651287.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论