So I've create two composables in vue 3. Both composable depends on each other for certain values.
useAuth.js
// Global State
const state = reactive({
isLoggedIn: false,
authToken: ''
})
export function useAuth() {
const { userData } = useProfile() // requires `userData` from `useProfile()`
const output = () => {
console.log('This is user data from useProfile:', userData)
}
return {
...toRefs(state),
output,
}
}
useProfile.js
// Global State
const state = reactive({
userData: {},
})
export function useProfile() {
const { isLoggedIn } = useAuth() // requires `isLoggedIn` state from `useAuth()`
const output = () => {
console.log('This is use login state from useAuth:', isLoggedIn)
}
return {
...toRefs(state),
output,
}
}
As you can see its a circular dependency, and when I run this code I get stack overflow message in browser console and the app crashes.
I was wondering, how to resolve such circular dependancy in vue 3 composables?
Update #1
I also tried extracting both composable into a third composable and tried using that in useAuth()
and useProfile()
but still facing the same issue.
useAuthProfile.js
export function useAuthProfile() {
const auth = useAuth()
const profile = useProfile()
return {
...toRefs(auth),
...toRefs(profile),
}
}