Тёмный

Vue JS 2 Tutorial #23 - Primitive vs Reference Types 

Net Ninja
Подписаться 1,6 млн
Просмотров 81 тыс.
50% 1

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

 

20 окт 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 56   
@SteveGreenwalt
@SteveGreenwalt 7 лет назад
Look no further for the best Vue JS tutorial on the web. Clear explanations and excellent examples.
@NetNinja
@NetNinja 7 лет назад
Thanks so much :)
@abdulazeezolanrewaju843
@abdulazeezolanrewaju843 5 лет назад
Attention, future watchers... if you get this error: "[Vue warn]: The data property "title" is already declared as a prop. Use prop default value instead. found in ---> at src/Components/header.vue at src/App.vue " Just make sure you don't return title in the data function again. That's basically what it's telling you. That the title is declared as props already, so don't declare it again in the data function.
@t1054384
@t1054384 4 года назад
Very simple, clean , without overwhelming unnecessarily informations. Great work!
@RohanMaheshwari7
@RohanMaheshwari7 7 лет назад
Seen many a video around here, and you are a definitely a top-notch teacher. Thanks for these videos!
@NetNinja
@NetNinja 7 лет назад
Thanks so much :). Really glad you like.
@kneeh
@kneeh 3 года назад
Great tutorials, liked and subscribed ! Just a small note, Strings are reference types. fixed bytes values (not including arrays) are primitives, for example integer (which is always 4 bytes in memory). The reason this example did work, is because strings are also immutable, meaning when you updated the string, you created a new one and updated the reference and not the actual value. So after the update, there are two string reference, one to the updated, and one to the original (now there are two strings in memory).
@jelodev-j1f
@jelodev-j1f 7 лет назад
How do we avoid this when dealing with reference types? Are we going to apply immutability rules like duplicating the array and storing it into another variable? Btw, good tutorials bro! Keep it up! Subscriber since the early days of your channel!
@dominiclapitan8466
@dominiclapitan8466 4 года назад
Very simple yet so understandable explanation! Thank you!
@yannmasoch
@yannmasoch 6 лет назад
Your videos are great and very well explained, I really enjoy them! For this video, you could have shown how to pass the "title" as a Primitive or as a Reference to see the 2 different ways for the same data. For example: title: "Vue Ninjas" vs title: { text: "Vue Ninjas" }
@sydneybecher4733
@sydneybecher4733 6 лет назад
nice tutorial man thanks I am facing this warn when creating direct method in header `Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders.`
@kerendn
@kerendn 6 лет назад
Your explanations are crystal clear. Brilliant.
@edwardkaminski3362
@edwardkaminski3362 7 лет назад
Hey, man. First off, thanks so much for making this series. It's awesome and much appreciated. I did notice at about 5:50 that you added the "title" prop, but didn't remove the "title" data member from Header.vue. If they're named the same, do props take precedence over data members? Or did Vue happen to throw you an error in the console that we didn't see?
@NetNinja
@NetNinja 7 лет назад
Thanks :). This was my mistake - I forgot to delete the data item. But yeah, in general, a prop will override a data item with the same name.
@powerpoint444
@powerpoint444 3 года назад
Awesome .. It's the best courses I can find on youtube for now..
@لبنانيأصيل-ص4ز
@لبنانيأصيل-ص4ز 3 года назад
Hello, I'd like to submit a feedback. While reference types are generally object with content that can be changed, the real difference between the two examples is "re-assigning" the prop: If you change the content of a prop (e.g. modifying the content of an array or an object), it'll change the object it's referencing too, however if you re-assign the prop (i.e. props = something else), it won't change the value of the actual prop that was passed from the parent component. Therefore, even with an array, re-assigning the whole prop won't change the passed prop from the parent component.
@thanasisbalatsoykas2389
@thanasisbalatsoykas2389 6 лет назад
if we all shared his videos he would start growing faster and get the views he deserves
@NetNinja
@NetNinja 6 лет назад
Thanks so much :)
@Plokm715
@Plokm715 6 лет назад
Thanks a lot, This Tutorial is the best Vue JS tutorial ever!
@pasizdobrekuce
@pasizdobrekuce 6 лет назад
You sound like Brian Cox. Which is brilliant because he's probably the greatest science communicator ever.
@NetNinja
@NetNinja 6 лет назад
Haha, thanks, I guess :). Were both Mancs so probably have the same accent. Believe it or not, we both studied physics at the same university too XD
@pasizdobrekuce
@pasizdobrekuce 6 лет назад
You're welcome. Well, the way you articulate and explain code is extremely clear and easy to understand, same as Brian Cox explains physics. Thank you for sharing all this "gold" for free.
@mohammadalongan693
@mohammadalongan693 5 лет назад
I've got a warning " [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "title" "
@timgehrsitz3267
@timgehrsitz3267 5 лет назад
If you're watching this and using TS and wondering how to fix the "this.items.pop();" giving an error, use "this.$props.items.pop();"
@mmakrzem
@mmakrzem 7 лет назад
in the header you added the title prop, but didn't delete the title variable defined in data. So does this mean that props always override data? Or is that only true because you defined props before data? When doing the on click event, is it changing the prop or data's title?
@sunsande
@sunsande 6 лет назад
(maybe you already have the answer, but here for the others who would ask the same ...) github.com/vuejs/vue/issues/1166 In the example from the video you get: [Vue warn]: The data property "title" is already declared as a prop. Use prop default value instead. found in ---> at src\components\Header.vue at src\App.vue (tested with vue.js version 2.5.16)
@husniddinqurbonboyev2634
@husniddinqurbonboyev2634 4 года назад
A brilliant explanation!
@onecarwood
@onecarwood 7 лет назад
Is it normal to keep the styles in the same file like you show? Or is this just for the videos to make it easier for us to see. i really like having everything for each component in the same file.
@NetNinja
@NetNinja 7 лет назад
Yeah - in Vue JS this is normal, and it easily allows us to scope the styles of each component so they do not affect other components too!
@onecarwood
@onecarwood 7 лет назад
Wow, that is what I was hoping and makes me like Vue even more. What an awesome little framework that is getting overshadowed by React and Angular where you have to make a bunch of files to do what Vue does in one. I wonder why it hasn't become the go to.
@arefmaleki9255
@arefmaleki9255 6 лет назад
I am really enjoying this tutorial. Thanks for such a great training. I've got a question: how can I change the title back to the original after clicking on changeTitle in "{{title}}"? like a toggle. Thanks
@dawnious
@dawnious 7 лет назад
Tell me if I'm wrong but I know that String must be reference type as well. Is it different in JavaScript?
@haeyongHwang
@haeyongHwang 5 лет назад
High quality lecture.
@adam192021
@adam192021 5 лет назад
The BEST as always!
@BorisBarroso
@BorisBarroso 6 лет назад
Really nice, learned something new
@RexGalilae
@RexGalilae 5 лет назад
Is there a way to explicitly deepcopy or copy by reference by, say, using something other than v-bind? Manually adding lots of data into every component would be too hacky, I'm guessing. There has to be another way
@VitaliiBahmet
@VitaliiBahmet 7 лет назад
Great clean tutorial! Many thanks, mate!
@NetNinja
@NetNinja 7 лет назад
Thanks :)
@seanraz
@seanraz 7 лет назад
I have a similar question to Joshua. I've done a bit of stuff in React and they're all about one-way, but editing a reference type in a child component seems to violate that pattern.... In practical terms when building apps to people use ref types this way from child components?
@peterhenry7661
@peterhenry7661 7 лет назад
I was asking myself the same thing. From what I can tell: NO, editing a reference type in order to mutate parent state is BAD in Vue 2 (this is a change from the approach encouraged in Vue 1). Source: vuejs.org/v2/guide/migration.html#Prop-Mutation-deprecated Of course, the question then becomes "How do I tell the parent about changes in the child?" and from what I can tell so far it has to do with emitting events. Still studying how to do that properly, hoping the next video (Vue JS 2 Tutorial #24 - Events (child to parent)) will get me unstuck. :)
@wahhajs
@wahhajs 7 лет назад
Nice video
@derickdaniel6914
@derickdaniel6914 5 лет назад
You didn't remove title from data() in header.vue, because now we are passing title from app.vue through props. Correct me if i am wrong. Thanks.
@lazymacs2823
@lazymacs2823 3 года назад
I also notice that
@kodieivie
@kodieivie 3 года назад
Is there a legit way to create a new object in memory when passing a reference type? I've come up with a clever way using set wrapped in an array but it still feels a little hacky...
@BobbySpencer-BBDGTL
@BobbySpencer-BBDGTL 4 года назад
*As of Jan. 2020, you will run into problems at around the 6:00 mark. Because 'title' is a key that has been used already in Header.vue, you will not be able to use in App.vue. Simply change or delete 'title' from the header file under data and it should work fine.
@solidkundi
@solidkundi 7 лет назад
I found a work around.. you can pass the "referenced" object to a variable ....using JSON.stringify and JSON.parse.. i was able to make a copy of it.... working so far..didn't play around too much
@meffesino
@meffesino 6 лет назад
Thank you very much! With this, i finally solved the problem I had to create a primitive of an Array!
@danielseer1435
@danielseer1435 7 лет назад
Very impressive presentation.
@danielgabe8501
@danielgabe8501 6 лет назад
Thankyou so much
@georgesmith3022
@georgesmith3022 6 лет назад
But in the component tutorial you said you define the data as a function and not as an object so that each instance has a different object
@saidulalam2840
@saidulalam2840 6 лет назад
[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "title" i have gotten this when check Primitive. THANKS
@rialf5373
@rialf5373 6 лет назад
thats warn, we let that or do something that vue warn disapper?
@Ace112p
@Ace112p 5 лет назад
@jimpauloovejera2599
@jimpauloovejera2599 5 лет назад
You don't mess with props values. Let the parent handle that for you
@sebastiansmoronski8854
@sebastiansmoronski8854 6 лет назад
This video seems no longer to be true. Example: jsfiddle.net/50wL7mdz/540402/ - here I pass primitive string variable to child components, and when parent's component data is changed, childs also update theirs data.
@alwajdi
@alwajdi 5 лет назад
"If parent's component data is changed, childs also update theirs" is true. But I think you need to watch carefully and pay attention to details. He wanted to change one of the child's data not the parent's. but if the prop is a reference type, it will also change the data in other components. but if the prop is a primitive type, the specific child's data can be changed without affecting other component's data
Далее
Vue JS 2 Tutorial #24 - Events (child to parent)
7:31
Просмотров 162 тыс.
Vue JS 2 Tutorial #21 - Nesting Components Examples
13:35
JavaScript Pro Tips - Code This, NOT That
12:37
Просмотров 2,5 млн
You might not need useEffect() ...
21:45
Просмотров 165 тыс.
This Is Why Python Data Classes Are Awesome
22:19
Просмотров 810 тыс.
Vue JS 2 Tutorial #26 - Life-cycle Hooks
9:18
Просмотров 100 тыс.
Go Pointers: When & How To Use Them Efficiently
14:09
The Weird History of JavaScript
12:09
Просмотров 1,2 млн