question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

help: how to use ssrRef with useFetch

See original GitHub issue

**📚 What i am trying to do? ** i really need use a shared ref with useFetch how can i do that

🔍 What have i tried? i tried this :

import { Ref, ssrRef } from '@nuxtjs/composition-api'
const sharedMap = new Map()

function sharedRef<T>(value: T, key: string): Ref
function sharedRef<T>(value: T, _?): Ref
function sharedRef<T>(value: T, key: string): Ref {
  const givenKey = key || value
  if (sharedMap.has(givenKey)) {
    return sharedMap.get(givenKey)
  }
  const newRef = ssrRef(key ? value : null, givenKey as string)
  sharedMap.set(givenKey, newRef)
  return newRef
}
export { sharedRef }

to avoid twice hydration, am i in right way? if not how can i do this?

ℹ️ Additional context i have another problem too, i want to check if my shared ref has data don’t fetch it with client navigation therefor :

const { fetch } = useFetch(() =>{
 if(mySharedRef.value){return}
} ...)

it’s totally fine in develop env but when i deployed it, data has gone after a quick showing, but page source is okay.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
danielroecommented, May 26, 2021

@ziaadini Great 👍

0reactions
ziaadinicommented, May 26, 2021

thanks. i had some issue with ssrRef and i fix it by this code with vuex

import { useStore, computed } from '@nuxtjs/composition-api'

export const useStoreRef = (initialValue, key) => {
  const store = useStore()
  return computed({
    get() {
      // @ts-ignore
      const data = store.state.refData[key]
      return data !== undefined ? data : initialValue
    },
    set(value) {
      store.commit('SET_REF', { key, value })
    }
  })
}


inside store/index.ts mutation:

 SET_REF(state, { key, value }) {
    Vue.set(state.refData, key, value)
  },

and every thing work as expected now and i can useFetch because i’m using vuex, not ssrRef. i think it’s okay now isn’t it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

help: Some problem with useFetch #18 - GitHub
I was trying to build a composable method where it has the state wrapped within a ref and some methods to get and...
Read more >
useFetch - Nuxt Composition API
You can access the Nuxt fetch() hook within the composition API. ... the state would be sent from server to client "twice", via...
Read more >
Handling server-side applications with Nuxt's Composition API
In this post, we'll take a look at how the Nuxt Composition API works and best practices for using it in projects.
Read more >
Vue to Nuxt: Server-Side Rendering Example
We can do rendering work on the server side a way faster so the user ... Switching refs to ssrRefs and useFetch to...
Read more >
Nuxt Composition API, updating 'state', not reflected on UI ...
Looking for directions on how to get this to work, improve any of the code i've currently setup. javascript · nuxt.js · vuex...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found