React Compound Components

  Рет қаралды 27,158

Karl Hadwen

Karl Hadwen

Күн бұрын

Пікірлер: 83
@CognitiveSurge
@CognitiveSurge 5 жыл бұрын
Hey 👋 what do you think about Compound Components in React? Do you have a favourite design pattern? Let me know! 👇
@outbacklife2025
@outbacklife2025 3 жыл бұрын
i make them on 8 different monitors
@adammescher
@adammescher 4 жыл бұрын
As I have stumbled down the Compound Components rabbit hole, this was exactly what I needed -- all of the pieces assembled in real-time narrated by someone who is capable. Thank you for your time and effort making this.
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
You are welcome Adam!
@martinfoakes4775
@martinfoakes4775 4 жыл бұрын
Good stuff! That use of the index.js file in your components folder is one of those things where, as soon as I saw it, I immediately wondered why I had never done that before
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
I had that same lightbulb moment happen to me a few months back 😆
@jasonmiani8108
@jasonmiani8108 4 жыл бұрын
^^
@keduslejiyared8701
@keduslejiyared8701 4 жыл бұрын
I learnt a lot from this video and I'm not even halfway through! Thank you, Karl.
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Glad it was helpful!
@lakshyamunjal5608
@lakshyamunjal5608 4 жыл бұрын
I have never heard of Compound components before and after watching this video, I totally understand it. Thanks Karl!
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Happy to help!
@cv3787
@cv3787 4 жыл бұрын
I tried to rewrite this in TypeScript but was unsuccessful. How do I write Card.Body etc without getting errors by TS?
@danva9273
@danva9273 3 жыл бұрын
This was my first React mini-project, simple yet informative, thanks!
@CognitiveSurge
@CognitiveSurge 3 жыл бұрын
Great to hear!
@DarrylHebbes
@DarrylHebbes 10 ай бұрын
Compound Components basically start at 9:10 mark
@kostya3312
@kostya3312 5 жыл бұрын
Thanks for useful tutorial. One note - className prop of Card component is overrided by className prop of Container component (through restProps destructuring). There is same issue for other components too. As a result there will not be classes 'card', 'card__body' and others in the final markup.
@CognitiveSurge
@CognitiveSurge 5 жыл бұрын
Ahh yes, my bad. If you switch from 'className' to 'classes' when passing down additional classnames, it should retain the set class that is set under the hood and allow you to additionally add anything on. Great spot my friend, will update the repo with 'classes' for the example on Card.
@LumbreraEnMiCamino
@LumbreraEnMiCamino 3 жыл бұрын
how can do this with React and typescript? including props and interfaces
@CognitiveSurge
@CognitiveSurge 2 жыл бұрын
Will show in a future video
@rofazayn
@rofazayn 5 жыл бұрын
After watching your video I realized how stupid the way I am using styled-components is 😂 Thanks Karl, great content as always.
@CognitiveSurge
@CognitiveSurge 5 жыл бұрын
Don’t worry, I did the exact same thing many times 😂 at first I wasn’t a fan because I wasn’t using them correctly, now I understand them, I love them 😀 thanks dude 🙌
@juliuscecilia6005
@juliuscecilia6005 4 жыл бұрын
Learned so much from this! Beginning to use this in my projects! Thank you so much! :D
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Great to hear!
@outbacklife2025
@outbacklife2025 3 жыл бұрын
sir instead of the joker image can i display a image of what th euser uploaded for like a apartment are room search?
@CognitiveSurge
@CognitiveSurge 2 жыл бұрын
Yep, can do
@govindkothari7989
@govindkothari7989 4 жыл бұрын
@Karl Hadwen i have one dought like for representing an image we have to use img tag so like how card.Image is replacing the img tag Or card.Image will convert to img tag depending upon what props we passed on card.Image
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
I'm not too sure what you mean?
@VitorBoccio
@VitorBoccio 3 жыл бұрын
why do u use the classname library? I mean, Styled components already create dynamic classes names and if you import from 'styled-components/macro' the class name will contain your component name as well.
@CognitiveSurge
@CognitiveSurge 3 жыл бұрын
I was being lazy, you can use that or just use export function classNames(...classes) { return classes.filter(Boolean).join(' '); }
@lukaszwalczak3814
@lukaszwalczak3814 3 жыл бұрын
Can someone please explain dot notation? What's the point of doing it?
@CognitiveSurge
@CognitiveSurge 3 жыл бұрын
It's a marmite pattern, some find it useful and others not so much. I was just experimenting with things mainly when researching the pattern
@lukaszwalczak3814
@lukaszwalczak3814 3 жыл бұрын
@@CognitiveSurge I like marmite. Especially on toasts :). I think my question should've been 'How does dot notation translate to JS world? Is Card.Title like a property of the Card component? Like in objects - object.property?' Cheers, mate.
@tallahondro
@tallahondro 4 жыл бұрын
Loved the video, still not sure how the dot notation is working under the hood for something like Card.Image = f(n). Do functions take on the same behavior as objects in JS? You are essentially declaring a function onto another function?
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Yep, pretty much that! Give this a read, it should help: blog.bitsrc.io/the-chronicles-of-javascript-objects-2d6b9205cd66#:~:text=Almost%20everything%20in%20JavaScript%20is,primitive%20values%20or%20primitive%20types. - apologies for the slow responsive!
@well.8395
@well.8395 4 жыл бұрын
In your styled/Card.js file - Line 42, there's an error. className={(classNames('card__image'), classes)} should be className={classNames('card__image', classes)}
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Oops xD
@user-uh3zr7mo4i
@user-uh3zr7mo4i 5 жыл бұрын
I really needed this, thanks Karl
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Glad I could help :)
@Skia_
@Skia_ 5 жыл бұрын
I'm loving the accent ;D Greetings from Lebanon.
@CognitiveSurge
@CognitiveSurge 5 жыл бұрын
🤣 thanks dude! Greetings from Leeds, UK! I hope all is going great and you have an awesome 2020!
@baldmannnnn
@baldmannnnn 2 жыл бұрын
Hi Karl! I'm able to understand and use compound component pattern on my react projects because of this tutorial. But now I'm trying to implement it on typescript and I'm having all sorts of problems. Can you make a video about compound component pattern with styled components using typescript?
@CognitiveSurge
@CognitiveSurge 2 жыл бұрын
Will take a look!
@buntybru3106
@buntybru3106 2 жыл бұрын
Thanks Karl
@CognitiveSurge
@CognitiveSurge 2 жыл бұрын
Np!
@emaxix7
@emaxix7 4 жыл бұрын
Great video! What's the name of the font-family you use on VSCode?
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
All in my setup video :)
@Abhishek-dp5tc
@Abhishek-dp5tc 4 жыл бұрын
font-smooth is unknown property.....it shows in codesandbox
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
There' are a few compatibility issues at the moment with font-smooth but they should be resolved in the future hopefully
@saisharathsamala5922
@saisharathsamala5922 3 жыл бұрын
Very good useful video 👌
@CognitiveSurge
@CognitiveSurge 3 жыл бұрын
Thanks a lot!
@inayatcassambai469
@inayatcassambai469 4 жыл бұрын
I feel like there isn't a major difference between the 2 examples you showed at the start of the video. I personally would prefer to go with the example on the left. Yes i would have to create individual component files for each element, but this way, each component would be encapsulated within it's own file which is something i prefer. The method you demonstrated has all the component logic in one file which makes it feel cluttered to me. But again, these are just opinions and everyone has their own preferred way of working! I could still have a folder in the components directory called cards. And then have all the relevant card components live in this folder accompanied by a index.js file. Then i can use this to import only the components i need like so: import {CardTitle, CardBody} from './components/cards'
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
You make a good point. I did use this method of design in the Netflix video I recently did, but on a larger scale, I'm not sure I like compound components, I'm on the fence. I'm half way through an Instagram clone and went back to just regular components and I think I'm actually enjoying using them more.
@inayatcassambai469
@inayatcassambai469 4 жыл бұрын
@@CognitiveSurge You may find this article interesting! epicreact.dev/soul-crushing-components/
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
@@inayatcassambai469 Will read that in the morning. Thanks for commenting btw, these type of discussions are one of the reasons I created this channel!
@amirnoorani5017
@amirnoorani5017 4 жыл бұрын
Extremely helpful!
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Thank you!
@omarhassan2548
@omarhassan2548 4 жыл бұрын
This is another great video Thanks Broo
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Thanks Omar!
@ManuelNyC77
@ManuelNyC77 4 жыл бұрын
I'm sorry, I can't understand why you use the functions in the components, as an example, Card.Body = function CardBody, I can't get why you use the function CardBody :/ can someone help me?
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
I use it to wrap all the other components inside and apply styles to it
@zourdy697
@zourdy697 3 жыл бұрын
mate would be great if you create content about advanced react patterns or context.
@CognitiveSurge
@CognitiveSurge 3 жыл бұрын
Definitely! Those are quicker to do too xD
@MaganDesigns
@MaganDesigns 3 жыл бұрын
My portfolio tanks you a million.
@CognitiveSurge
@CognitiveSurge 3 жыл бұрын
haha
@iali1046
@iali1046 5 жыл бұрын
Awesome 👏
@merveillevaneck5906
@merveillevaneck5906 4 жыл бұрын
im failing to see what exactly the implementation benefit is to this approach on this specific example? can anyone maybe help my stupid ass see the benefit?
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
This comment did make me lol...it's basically just a pattern that's used in design libraries, so for example if you had 10 people using one of your comments but they wanted only certain aspects, they could just pull in the one component and then get the stuff they need, e.g. - and use it as it, you can do the same with individual components but it becomes a bit tedious. But all in in, it's just a pattern and up to the user to decide which they like best :) hope that helps!
@Abhishek-dp5tc
@Abhishek-dp5tc 4 жыл бұрын
make more videos on styling in React, Also you can make on Tabs, Accordion(components)
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Tailwind videos coming but also much more CSS videos!
@Max14211
@Max14211 4 жыл бұрын
@@CognitiveSurge Aw man that's going to be dope im looking forward to it :D. Just to pick your brain for a second, I heard the some of the team-members who where part of tailwind created ThemeUI as a sort of successor to tailwind. I was wondering, on a production level, do you think that tools like Tailwind will just become a sort of passing fad and people will move into more barebones tools like ThemeUI or do you think that startups and mid-size companies will prefer heavier CSS frameworks going forward and that tailwind is more for smaller projects? P.S Just wondering your thoughts, its the tech world and no one predicts it right LOL so i'm not going to hold you to perfection or anything just your gut reaction to a random question lol. Cheers for all the videos :)
@EcklerV
@EcklerV 4 жыл бұрын
Can't you just write like this? Card.Title = Title. Isn't it the same? I mean styled component basically takes it's props and pass them down or am I mistaken? Do you do it just to add classname ?
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Yep, I did it just to add classname, but now I don't use classNames at all. I've removed it completely and handle all my styling via a styled component
@EcklerV
@EcklerV 4 жыл бұрын
@@CognitiveSurge Could you please share a chunk of code that illustrates your new approach? I'm new to styled components and trying to figure out the best way to use them. Thanks in advance!
@BrunoWinck
@BrunoWinck 5 жыл бұрын
Another great one, keep sharing :)
@CognitiveSurge
@CognitiveSurge 5 жыл бұрын
Thanks Bruno! Hope you're well and happy new year!
@avneet12284
@avneet12284 2 жыл бұрын
Didn't realize that adding a dot makes them children. WOW ! 😮
@CognitiveSurge
@CognitiveSurge 2 жыл бұрын
:)
@abiakhil69
@abiakhil69 4 жыл бұрын
Awesome content
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Thanks dude
@unknownman1
@unknownman1 4 жыл бұрын
Hey Karl sorry to say this, but didn't understand the concept clearly. I came here because in Netflix tutorial you've used styled components.
@unknownman1
@unknownman1 4 жыл бұрын
Alright, I read the docs and watched the video again and understood the concept now. Thanks Karl.
@CognitiveSurge
@CognitiveSurge 4 жыл бұрын
Glad you understood it dude! I'm moving or I'd have been able to help more!
@kewalos96
@kewalos96 4 жыл бұрын
Seems cool, but I don't think it's for me.
Compound Components in React (Design Patterns)
18:21
Cosden Solutions
Рет қаралды 52 М.
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
Un-Suck Your React Components - Composable & Compound Components
15:47
Why you should look into these React component styling options!
27:17
Every React Concept Explained in 12 Minutes
11:53
Code Bootcamp
Рет қаралды 944 М.
Compound Pattern in React | #30 React Course
11:35
Code with Sloba
Рет қаралды 9 М.
Using Composition in React to Avoid "Prop Drilling"
15:42
React Training
Рет қаралды 88 М.
Compound components pattern in React
13:52
Akilesh Rao
Рет қаралды 1,6 М.
10 React Antipatterns to Avoid - Code This, Not That!
8:55
Fireship
Рет қаралды 786 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23