Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Replace usage of useContext with use... #645

Closed
1 task
dhruvkb opened this issue Jan 20, 2022 · 5 comments · Fixed by #1418
Closed
1 task

Replace usage of useContext with use... #645

dhruvkb opened this issue Jan 20, 2022 · 5 comments · Fixed by #1418
Labels
💻 aspect: code Concerns the software code in the repository ✨ goal: improvement Improvement to an existing user-facing feature good first issue New-contributor friendly help wanted Open to participation from the community 🟩 priority: low Low priority and doesn't need to be rushed

Comments

@dhruvkb
Copy link
Member

dhruvkb commented Jan 20, 2022

Description

Nuxt recommends the use of specific use... functions like useRoute, useRouter, useStore etc. instead of destructuring from useContext.

Additional context

These replacements will smooth the future upgrade to Vue 3 with Composition API.

Resolution

  • 🙋 I would be interested in resolving this bug.
@dhruvkb dhruvkb added good first issue New-contributor friendly help wanted Open to participation from the community 🟩 priority: low Low priority and doesn't need to be rushed ✨ goal: improvement Improvement to an existing user-facing feature 💻 aspect: code Concerns the software code in the repository labels Jan 20, 2022
@obulat
Copy link
Contributor

obulat commented Jan 20, 2022

We had a problem with using route destructured from useContext not working correctly, and @krysal fixed it in #588.
Other than that, we seem to only use useContext for getting app (to get the i18n.localePath), i18n and store in the code. The i18n plugin will probably be imported using something like this:

import { useNuxtApp } from '#app'
  const { i18n } = useNuxtApp()

And the store can be imported using useStore(), but since we are planning to use Pinia later, it might not be worth the effort.

Are there instances of useContext that are causing problems, @dhruvkb ?

@dhruvkb
Copy link
Member Author

dhruvkb commented Jan 20, 2022

I haven't looked at the individual usages. I just saw it being used extensively in the code and that it was not recommended so I opened the issue.

As for Pinia, I read somewhere that it will become Vuex 5 so they might retain the useStore construct. But yeah, it might not be worth the effort if they rename. Hence the "priority: low" and "good first issue" labels 😆 .

Also, none of the usages are causing problems, just wanted to align with Nuxt recommendations.

@obulat
Copy link
Contributor

obulat commented Jan 20, 2022

The recommendations are to not use useContext for route and router, other usages are OK (there is no alternative, actually). So, I would vote for closing this issue - if others agree.

@sarayourfriend
Copy link
Contributor

@dhruvkb I think the latest guidance from the Vue project is to just switch to Pinia directly instead of waiting for Vuex 5:

https://twitter.com/VueDose/status/1463169464451706897

@sarayourfriend
Copy link
Contributor

For this it would also be great to create our own useStore and useI18n hooks. Nuxt bridge does not have shims for useStore as present in Nuxt 2 and retriving the i18n object requires useNuxtApp().$i18n. For now we can continue to use useContext().i18n in that function and then the upgrade to Nuxt bridge will be easier because we can just swap the implementation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💻 aspect: code Concerns the software code in the repository ✨ goal: improvement Improvement to an existing user-facing feature good first issue New-contributor friendly help wanted Open to participation from the community 🟩 priority: low Low priority and doesn't need to be rushed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants