Тёмный

Is your function REALLY a Vue composable? 

Alexander Lichter
Подписаться 10 тыс.
Просмотров 12 тыс.
50% 1

Опубликовано:

 

5 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 74   
@Lannnister
@Lannnister 10 месяцев назад
I always wanted to learn the difference between a composasble and just utilitiy functions. Thanks! this was super clear.
@TheAlexLichter
@TheAlexLichter 10 месяцев назад
Glad I could help you out on this 🙏🏻🙌🏻
@sandros94
@sandros94 11 месяцев назад
Damnit, I already see myself next week refactoring a bunch of code...
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
Haha, I totally understand that 😛
@NathanChase
@NathanChase 11 месяцев назад
If you have composables in your composables directory, where would you put the non-composables? Would you make .ts files that sit right in the same folder with your .vue SFC files, or would you put them in some other "utils" or "helpers" folder? I find when I often reach for a composable is when my .vue file's script is getting large and I want to offload some larger functions into separate files, or if I find that there's one or more functions my component is using that I want to reuse for other components. I'm also not the biggest fan of having to always prepend "use" - semantically, it often doesn't make any sense to name it "useThing" - so I find myself really not wanting to use that naming convention - even if it has become a common practice. I wish there were a lot more videos on conventions for organization of things, because it feels like - even as much as Nuxt does in choosing sensible defaults and conventions - people still find ways of forging their own path for certain things and ways of organizing their repos and code.
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
Hey Nathan 👋🏻 I usually put functions that are no composables but should be reused into a `utils` folder indeed - grouping them by "concern". For composables, I mainly go for `use` or. `on` which *should* cover most scenarios. If you have some where it feels "unnatural" or "wrong", I'm curious to know more! Planning some more code organization video, especially at some point when Multi App Support for nuxt lands 🔥
@NathanChase
@NathanChase 11 месяцев назад
A few more directories I almost always use (that aren’t necessarily standard Nuxt 3) are: /types (for all your .d.ts declared type interfaces) /packages (If your repo is a monorepo, shared package.json devDeps go in here) /config (I like to pull out any huge chunks of nuxt.config and put them in individual config .ts files here)
@Shulkerkiste
@Shulkerkiste 11 месяцев назад
I always outsource *everything* which doesn't interact with Vue into a script file. Therefore, the Vue files are much shorter and easy to read. My folder structure could look like this: scripts folder: users.js authentication.js dashboard.js todos.js helpers.js composables folder: users.js event-listeners.js ...
@marcin_nieznaj
@marcin_nieznaj 11 месяцев назад
Great job, please make these more insightful Nuxt videos
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
Thank you Marcin 🙏🏻 I'll keep them coming! Is there anything specific you'd like to know more about in the Vue and Nuxt world?
@marcin_nieznaj
@marcin_nieznaj 11 месяцев назад
@@TheAlexLichter I deal with very project specific issues on daily basis so nothing in particular interests me at the moment. But seeing you go deeper into various Nuxt details by itself would be great
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
@marcinnieznaj5666 will put more “nuxt in-depth” content on the list 👍🏻
@Microphunktv-jb3kj
@Microphunktv-jb3kj 10 месяцев назад
6:45 , i guess thats why nuxt framework has 2 folders... one for composables.. and other utils for just helper/utility functions... :D
@TheAlexLichter
@TheAlexLichter 10 месяцев назад
Exactly 👌🏻
@chstappert
@chstappert 11 месяцев назад
You have a hand in the topics which matter the most.
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
I'm very happy that you think so! 🙏🏻Hope the examples helped understanding when a Vue composable is a composable 😋
@brahim_boussadjra
@brahim_boussadjra 11 месяцев назад
Thank you for this informative video. I think the best name of composables that hold only lifecycle hooks is onX instead of useX, also you forget to add dependency injection in the checklist
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
Hey Brahim! Good points. I would use the `on` prefix for "reactions", e.g. onKeyPressed or onResumePage, which are not necessarily tied to lifecycle hooks but more as "reaction to changes/state". ☺️ DI is a good point indeed - though that also means dealing with state in a way so I might get away with categorizing it into the "stateful" rule here 😇
@brahim_boussadjra
@brahim_boussadjra 11 месяцев назад
​@@TheAlexLichter thank you for reply
@olatunjiolakunle6908
@olatunjiolakunle6908 11 месяцев назад
This video answer the question I ask the other day on one of your videos, Thanks Alexander
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
Any time 🙌🏻
@rahman9418
@rahman9418 11 месяцев назад
Waiting for the next video, I will always look forward to it, Thank you very much Alexander
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
Delighted to hear that 🙏🏻 any favorite video so far? ☺️
@rahman9418
@rahman9418 11 месяцев назад
@@TheAlexLichter everything
@Ayvengo21
@Ayvengo21 3 месяца назад
That would be more usefully to get some examples of common problems solved by composable. Like for example should i made some of those as a wrapper or maybe a plugin would be better let say for a case when i have to check for 401 every time. Where else composable could make my life easier and just not over complicate an app for no good reason.
@виртуоз_ру
@виртуоз_ру 11 месяцев назад
Вопросов нет. Всё ясно и понятно. Благодарю 👍
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
Рад это слышать, приятель 👏🏻
@OttoPaulsen
@OttoPaulsen 10 месяцев назад
Thank you for a clear and good explanation. I was wondering about that user function that you determined was a composable, and renamed to useUser. That user ref in there, should not that normally be defined outside the function? At least if you want all the users of that composable to share it?
@TheAlexLichter
@TheAlexLichter 10 месяцев назад
Hey Otto! Thank you for the kind words 🙏🏻 It depends a bit what it is used for, but yes, putting the ref outside would create global state, which can be desired in this case! When using Nuxt though, I'd rather go with useState + keep it inside because that way I create global state without "leaving the Vue context" 😋
@chiebidoluchinaemerem5860
@chiebidoluchinaemerem5860 10 месяцев назад
"The mouse on the page" got me real good 🐁
@TheAlexLichter
@TheAlexLichter 10 месяцев назад
Haha, glad you spotted and liked that 😁
@joshdeltener2012
@joshdeltener2012 11 месяцев назад
// is this a composable? Even though it's a bit silly, I say yes! Technically it has state. const keys: Record = {} export default function (key: string, callback: Function) { if (keys[key] === true) return keys[key] = true callback() }
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
I'd say it is no composable as managing the state happens through exposing global state and not through reactive references 😋 You can easily run it outside of the Vue context in any browser or Node enviroment 👍🏻
@Tarabass
@Tarabass 10 месяцев назад
One of the rules I miss is re-usability. I see composables like some kind of mixins and composables should only be created when the functionality is re-used, like so for mixins. So I would like to add a rule: Is the function used in multiple places? Correct me if I'm wrong :)
@TheAlexLichter
@TheAlexLichter 10 месяцев назад
Hey! Yes and no. You can also have composables that are extracted but live in the components (or an extra file) and are not reused, purely for grouping ☺️
@bernattousfiga
@bernattousfiga 7 месяцев назад
Great explanation and tips Alexander! I'm doing a bit of refactoring now 😅 What about the classes? Should I check the checklist for all methods of the class and, if at least one method acomplish one of the conditions, it means the class is a composable? How would you approach it? Thanks in advance!
@TheAlexLichter
@TheAlexLichter 7 месяцев назад
To be fair, I almost never use classes as abstraction in the frontend. I'd suggest to make them as "composable-free" as possible and use actual composables to work with classes, e.g. as I've shown with the repository pattern ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-jXH8Tr-exhI.html
@bernattousfiga
@bernattousfiga 7 месяцев назад
@@TheAlexLichter Thanks for the answer! I'll try to follow these suggestion 😊
@irinakats8008
@irinakats8008 5 месяцев назад
Thanks, after reading the docs I couldn't see the difference, now I do
@TheAlexLichter
@TheAlexLichter 5 месяцев назад
Glad I could help 🙌🏻
@moe8055
@moe8055 10 месяцев назад
Thanks alot Alex! Greets from Düsseldorf
@TheAlexLichter
@TheAlexLichter 10 месяцев назад
My pleasure! Greetings back from AMS 🙌🏻
@MuhammadKhizar7
@MuhammadKhizar7 11 месяцев назад
Thanks for the video. what about using composables as service layer (fetching data) and then using it in setup? Will it breach convention?
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
If you handle the state (think of sth like useFetch) it is fine If it is just a fetch function (like the Fetch API or $fetch), it would be a utility!
@andogrando487
@andogrando487 6 месяцев назад
I've always struggled with deciding when something is a composable and when I should be using pinia or something to manage... maybe there is no case for pinia anymore since you technically could do almost all of it with a composable....
@TheAlexLichter
@TheAlexLichter 6 месяцев назад
Pinia is eventually also a "very refined composable" :D And it still can be valuable, depending on what kind of app you build and what needs you have. Especially plugins, devtools integration, a clear data flow etc. etc. can be pretty helpful for the DX.
@The4tticuz
@The4tticuz 8 месяцев назад
Thanks for that great video which makes it really clear. But I guess there is another question: is your composable a composable or a plugin? Can you help me with this or is it planned for another video? Thanks alot
@TheAlexLichter
@TheAlexLichter 8 месяцев назад
Hey! Interesting - Do you have an example where it is rather unclear?
@The4tticuz
@The4tticuz 8 месяцев назад
@@TheAlexLichter it's more a general question referring to your question (util function vs composable). I find myself some time getting stuck in trying to answer to myself is this particular function might be set up as a plugin or a composable. And I think it's not that easy... or Im just overthinking those concepts. A simple example could be: fetching data like products, which are only needed on that particular page, not stored and/or used in other components
@TheAlexLichter
@TheAlexLichter 8 месяцев назад
@The4tticuz I’d mostly go for a composable in this case. A plugin is useful if you need to provide a „global helper“, eg a custom $fetch instance 👍🏻
@MissedWarrior
@MissedWarrior 11 месяцев назад
Hey! Great video, thanks for it. What do you think about inject? Will it turn function to composable? I think yes because it requires vue injection context
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
Yes! Definitely also a composable - provide/inject is another way for handling state so that counts too 👌🏻
@markcasiano6501
@markcasiano6501 4 месяца назад
Hello, I am coming from your video "You are using useFetch WRONG!", and this is my scenario. I wrote a couple of composables that uses useFetch, which is a real composable(as per this video). Now, some of these composables uses that composable(nested). But I am going back to the problem "Component is already mounted, please use $fetch instead." which will be fixed if I use $fetch. My question is, if I change useFetch to $fetch, does that mean it is not composables anymore? Thanks.
@TheAlexLichter
@TheAlexLichter 4 месяца назад
Using a nested composable seems fine. The question is where the top-Level composable itself is called 👀
@markcasiano6501
@markcasiano6501 4 месяца назад
@@TheAlexLichter I get it, I found out that the warning was caused by watchEffect(), because it is executed twice on a direct page load.
@EvestDev
@EvestDev 4 месяца назад
Hi Alex, i have a question. I think i use composables a bit wrong. I use them just for the autoimport sake. For example i have one super simple one that just resolves a promise after a given number of miliseconds. I used them like some helper functions. But the question is, where should i put those files in my nuxt project if not in the composables directory?
@TheAlexLichter
@TheAlexLichter 4 месяца назад
Hey Evest 👋🏻 Yes, that's not entirely as intended. You have "utility functions" there which I'd put in the utils folder. These shouldn't have a "use" prefix but are also auto imported! 😋
@m0ksem
@m0ksem 11 месяцев назад
Hi. You said composables can be used in lifecycle hooks. Do you have a good example of this scenario? I feel like composables should compose component only in setup fn or other composables.
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
I wouldn’t advise it but the *can* in some scenarios (according to the docs) I think I never used it but possibly when working with SSR and depending on window or document - but there are IMO nicer workarounds there
@matanon8454
@matanon8454 11 месяцев назад
This helped me so much thanks 🙏
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
Happy to hear that! You are welcome 🙌🏻
@syahminawi4733
@syahminawi4733 2 месяца назад
Question! if 2 component calling the same composable, did it create different instance of that composable or they are sharing the stateful value?
@TheAlexLichter
@TheAlexLichter 2 месяца назад
Same as with JavaScript functions -> a different instance. If you use a ref *outside* of a composable though, it acts as global state. This shouldn't be done when using SSR though (for that you use useState).
@Microphunktv-jb3kj
@Microphunktv-jb3kj 10 месяцев назад
8:10 , if u didnt know computed is also ref , u only mention user as stateful logic... eventho computed is not ment to be mutated/create side-effects
@TheAlexLichter
@TheAlexLichter 10 месяцев назад
If you have a computed in a composable, then somewhere in there should also be a ref 👀 Can be as input or as initialized variable ☺️
@nishantrai982
@nishantrai982 4 месяца назад
why am I able to use the useFetch composable inside a util function in /utils directory?
@TheAlexLichter
@TheAlexLichter 4 месяца назад
Nobody "formally" restricts you to use it in `utils` *but* your "util" is then a composable. So it is not correct 😋
@jesusguerrero9515
@jesusguerrero9515 11 месяцев назад
Thanks for the info. Very clear.
@TheAlexLichter
@TheAlexLichter 11 месяцев назад
Glad it was helpful!
@anthonynicholls7
@anthonynicholls7 7 месяцев назад
If my composable has global state in Nuxt 3 or uses say a pinia store would that make it a composable?
@TheAlexLichter
@TheAlexLichter 7 месяцев назад
Yes, absolutely!
@youhan96
@youhan96 9 месяцев назад
Clear as mud sir! Just kidding
@TheAlexLichter
@TheAlexLichter 9 месяцев назад
😜 Hope it helped ☺️
@youhan96
@youhan96 9 месяцев назад
@@TheAlexLichter I kind of khew them but how you put that into 3 simple rules was super clear.
Далее
The definitive guide to shallowRef in Vue
11:52
Просмотров 6 тыс.
Build your own Custom Composables in Vue
12:13
Просмотров 42 тыс.
This mother's baby is too unreliable.
00:13
Просмотров 13 млн
What you're doing wrong with Vue.js 🙅‍♂️
9:37
Why you should use useState()
17:57
Просмотров 18 тыс.
BEWARE these Laravel attributes | Real World Laravel
19:10
useAsyncData vs. useFetch 🤯
15:20
Просмотров 10 тыс.
You are using useFetch WRONG! (I hope you don't)
11:14
A Better Way To Organize Components In Vue
10:10
Просмотров 22 тыс.
The BEST way to proxy your API in Nuxt
21:12
Просмотров 17 тыс.
Vapor: The Future Of Vue
21:27
Просмотров 121 тыс.