javascript - Deconstructing values nextjs: ReferenceError: Cannot access 'XXX' before initialization - Stack Ove

I am using Apollo Client with Nextjs and would like to deconstruct values for easier reading.I am tryi

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
Add a ment  | 

1 Answer 1

Reset to default 3

You'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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745277917a4620142.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信