No video

Using Composition in React to Avoid "Prop Drilling"

  Рет қаралды 86,481

React Training

React Training

Күн бұрын

You can compose your React components in different ways to avoid passing props down multiple levels, thereby increasing your component's flexibility PLUS making it more explicit.
This video was made in response to some of the replies to this tweet: / 1195431511417208834

Пікірлер: 205
@leerob
@leerob 4 жыл бұрын
For simple examples, I think this works really well. I'd argue for larger examples Context would work better. The solution with composition ends up creating a larger JSX tree at the top level, which again, is fine while it's small. The problem compounds as you continue to get components of components of components. I think in an ideal world there's a little bit of both. But I agree with what you're saying in theory - you don't need global state to avoid prop drilling.
@MauricioAndrian
@MauricioAndrian 4 жыл бұрын
Exactly my thoughts. Many apps are huge. I had the chance to use composition but I end up with many many nested components and it was awful to read and to find things quickly. One should use whatever it works for the problem at hand, and not to worry too much about "un/popular opinions".
@inambe3987
@inambe3987 4 жыл бұрын
You can create container components for handling the big markup, but Composition just feels the right way, IMO.
@RommelManalo
@RommelManalo 4 жыл бұрын
Agreed with Lee
@mjijackson
@mjijackson 4 жыл бұрын
This is just a technique, not intended to solve every situation! I just wanted to demo what it means to "use composition instead of context" in certain situations. I think that sentence is a bit difficult to digest if you're not familiar with the concepts.
@Cre8teWebDevelopment
@Cre8teWebDevelopment 4 жыл бұрын
I agree with Lee, the example is too simplistic - even in the example you used you also then need to pass the state setters down to the children elements, in your case LoginForm - what then happens if the login form itself is 3 or more children deep? Even though you are rendering components you end up defining most of the application in one place simply to avoid using context? It needs to be a combination of both composition and context. Users should be in context - there will rarely be only one child that needs it details - usually you will need the user to manage permissions as well. I think composition should be limited to anything that doesn't need to be outside of the current branch - like a dashboard tile, you would render a that makes sense, defining your content tree in doesn't.....
@vinodloha
@vinodloha 4 жыл бұрын
Thanks for making this so clear, I also always felt children pattern is most powerful feature of React and underrated too.
@GFe11
@GFe11 3 жыл бұрын
Great video! Although your keyboard drilled my ears more than props to my component 🤣
@carlsvv3200
@carlsvv3200 Жыл бұрын
Same here, but after i while i find it quite satisfying 🤣
@hgezim
@hgezim Жыл бұрын
My thoughts exactly! He's mean to his keyboard 😂
@omaraguinaga1322
@omaraguinaga1322 4 жыл бұрын
You did great here, when I first read the tweet I was very confused on how to use component composition instead of context and you made it so clear with this. Thank you so much.
@uobislrt
@uobislrt 8 ай бұрын
This video makes it just so easy to understand how to use component composition to get around prop drilling (and provide greater component flexibility). And only 15 mins! Thank you so much for putting this together.
@davidrocky
@davidrocky Жыл бұрын
This is great! My brain was already seeing that that Context seemed too much but I didn't tried composition until now. This surely will help me in killing that poor contexts. Seeing the comments I think this is the correct resume about using Composition vs Contexts: -Use Composition to solve simple (or shallow) prop drilling problems or to make components that can be composable and used in different places with some differences in the template. -Use Context when I really have some deep tree and this context is really need and used by many others components.
@grimmdanny
@grimmdanny 2 жыл бұрын
This is a good video. My opinion on this is that your example works well because it's extremely basic in UI design (i.e. none). Breaking down components like this into Children is something I've done a lot of, however I stopped doing this (exclusively) because when it came time to make something more complex - *like a real application with a real UI* - composition just wasn't cutting it on its own. Thanks to Context, I was able to get a lot more done, with a lot less code, and it helps keep my components sane. I've never had an issue with using Context to store large sets of data for the entire app, even in production, which in my case needs to be used in multiple components - with no relation to each other, and also deeply nested. I also never had a rendering issue with Context when applied to all child components under the provider either. I am not worried about the re-renders from the props being passed to all elements, none of it is being written to the DOM unless actual data changes. Composition became a problem for me when the UI I was developing started getting complex in layout, and I had too much code in the main App.tsx file trying to create this layout. Doing it all by Composition put me in a mindset where I started breaking out ALL small chunks of elements into their own Components and it was becoming a huge mess because I was convinced this was the best way to solve an issue. I eventually got over it once the product I helped developed removed a lot of Composition and instead replaced a lot of with Context with absolutely zero issues. It's in production, everyone is happy, and the product is profitable (in the >$900k a month). The biggest benefit to this is: Less developer headaches, quicker development because we simply get the data we need, when we need it and where we need it. While the Composition reduces a lot of prop drilling, you're really just replacing it with a lot of Child nesting. Either way, nesting needs to be managed. Today I use a mix of both and never had any issues since.
@kentbull
@kentbull 9 ай бұрын
I really like this. Composition makes a lot more sense when you want to share context across a set of components. Making things explicit increases maintainability and reduces potential bugs when components are reused.
@Xhisorz1
@Xhisorz1 2 жыл бұрын
Being able to pass state from the App component a few levels down is a great thing. I feel like this is going to help me fix a few issues with my code. Thanks for teaching this to us.
@alexandersen2718
@alexandersen2718 6 ай бұрын
Four years later, this saved my day! Thanks.
@thelostvorg7805
@thelostvorg7805 Жыл бұрын
so by using composition we explicitly expose the children of a component and pass state directly to the child component that needs it. this was a great video , I learned about composition and useContext hook at the same time . great !
@VideoWow7184
@VideoWow7184 3 жыл бұрын
Great video Michael. Even though this example is simple I think it still applies in larger apps such as in places with smaller compositions. For larger compositions (maybe you should think about why it's large), you could consider using a layout/template component with slots (props that take components) that abstract away things that might be common between dashboards for example (taking from the example in the video). You might end up with one level of prop drilling but I think that is still a better tradeoff than using context. The issue I have with a child component relying on a bit of context is that it's not immediately obvious and you so need to examine the inner workings of a component to figure out, firstly that it needs context, what context it needs, and its shape. I'm still learning about when it's best to use context but generally, I consider it for cross-cutting concerns; which passing a value to a child component is not imho.
@IdoSchacham
@IdoSchacham 3 жыл бұрын
This works only when the component that needs the property is relatively shallow in the tree. What if the component that needs the user is 10 levels deep? Using compositions at the very top level would end up in cluttered code. BTW, having a "black box" component isn't necessarily a bad thing. It hides complexity at one level and reveals it at another. Isn't that part of the idea of components?
@SandraWantsCoke
@SandraWantsCoke 7 ай бұрын
black box adds more complexity if you need composition (composition in this case means at some places you need to render it with a title, in some other without a title)
@jpljayapal
@jpljayapal 4 жыл бұрын
my understanding in use of context and composition applied in projects: Composition is best one while it's being used in a component which has lots of presentational components that expects values as props. Looks more code you have in a file. It's worth for future updates. Context can be used on top of composition to get specific data (not all, we have state for it) from very high level or root in large scale applications. Thanks for explaining more use of context.
@cypriankamilnorwid7777
@cypriankamilnorwid7777 2 жыл бұрын
This is great understanding! Good thinking dude!
@slowcoding
@slowcoding Жыл бұрын
The video helps me understand the difference between the Context and the function composition. Thanks a lot.
@sethreno3586
@sethreno3586 2 жыл бұрын
The "prop drilling" vs "use context" debate reminds me of "dependency injection" vs "static service locators" in the dotnet world. We often recoil at passing variables down complex dependency chains so we go back to using what are essentially global variables. This makes our components unintuitive and fragile. The complexity doesn't go away it's just hidden, but not in a helpful way where you don't have to think about it anymore.
@abubakarkhawaja6082
@abubakarkhawaja6082 2 жыл бұрын
Thanks for showing how to implement Composition. Now would be able to use both according to condition.
@sauravkumar007
@sauravkumar007 4 жыл бұрын
A very simple example to understand the scenario where we need to use context and component composition.
@brandonwie4173
@brandonwie4173 2 жыл бұрын
Thanks Jordan for this video. It really gave me a different view when dealing with props in components.
@tshddx
@tshddx 4 жыл бұрын
The problem is that a lot of components are primarily useful because they know what to render given some data. If you have to pass a big tree of children into Dashboard every time you use Dashboard, it’s really not doing much for you. In this simplistic example, it’s just rendering a heading then rendering its children.
@jaimesangcap6134
@jaimesangcap6134 4 жыл бұрын
I think you can make specific Dashboard then. Like or which has different children structure. See reactjs.org/docs/composition-vs-inheritance.html
@StiekemeHenk
@StiekemeHenk Жыл бұрын
But at the same time, the dashboard is now responsible for all content within it without overly abstracting things away. You can clearly and cleanly see what you've got. I was skeptical but thinking about it again, it seems great. And if you do have some components that take props for their state, why don't you just give it only to those components that need it? You still have a clean structure and less prop drilling. It just doesn't make sense to give data to components that are just going to pass it on.
@DesaraeVeit
@DesaraeVeit 7 ай бұрын
Or create a design system with compound components using the same methods and Walla… little to no props visible in the main app.
@blue_mustang_
@blue_mustang_ 2 жыл бұрын
Good approach.Using children prop this way it's useful to make the father component more customizable in case you need to render different children depending the situation.Thanks.
@htmlfivedev
@htmlfivedev 2 жыл бұрын
Best video on the topic ...just Excellent!!
@tubitubi10000
@tubitubi10000 4 жыл бұрын
It has been like 5 years that React released Open source, and we still don't know how to use the basics. Thank you for remembering us that! Cheers
@shamlikiruba
@shamlikiruba 4 жыл бұрын
Very clear video about where to use context and where to use composition. Thanks!
@hnsbro
@hnsbro 3 жыл бұрын
Thanks for the vid. Composition is one more tool/pattern we can use when appropriate. The 'prop drilling' issue aside - sometimes it's useful to use a 'blackbox' approach - for example when consumers of your component do not need to know or want to know about it's internals. Sometimes as a dev all I care is that if I feed it x, y and z the component does it's job.
@oliversaxon8656
@oliversaxon8656 Жыл бұрын
This has given me a new perspective - thank you!
@balazsbb
@balazsbb 4 жыл бұрын
Reposting my comment on Reddit here: This video has been very interesting to watch as someone with a background in C# and Angular. The problem here is exactly the same one dependency injection solves. What Michael does is extracting a dependency (`currentUser`) and manually assembling the composition root. Using Context would be what the DI world calls the service registry anti-pattern. I'm learning React right now and I find its simplicity refreshing. I would love to see if there are other ways of achieving dependency inversion in the React world. I honestly find the heavyweight approach of Java/C#/Angular DI containers distasteful.
@jamesjohnson5386
@jamesjohnson5386 4 жыл бұрын
If you use something like redux you get inversion of control out of the box without the need for di only because dicision making is abstracted away from all your components and the only things your components get injected with are actual data and callbacks
@DC-xt1ry
@DC-xt1ry 2 жыл бұрын
Very clear and concise! I will have the composition pattern in mind next time! thx for sharing
@MM-fh2ss
@MM-fh2ss 4 жыл бұрын
If you need to place this composite dashboard in several places, you will encounter code duplication.
@CanRau
@CanRau 4 жыл бұрын
You could wrap the dashboard block in a separate component which accepts the user prop
@benlorantfy6179
@benlorantfy6179 4 жыл бұрын
@@CanRau Then you're back with the original problem
@babri1402
@babri1402 3 жыл бұрын
@@benlorantfy6179 Not really. By extracting that block into a component 'UserDashboard', the new component will directly be passing that user to WelcomeMessage. The concept is very similar to nested if conditions Vs combining conditions via AND into a single if. The result is a much simpler & easier to understand code without a stair of brackets. Take a look at React documentation showing a similar example for Dialog & SignupDialog: reactjs.org/docs/composition-vs-inheritance.html
@coder3123
@coder3123 2 жыл бұрын
Thank you for this explanation.
@dadou9590
@dadou9590 7 ай бұрын
Composition in react works well with a dynamic component that parse json recursively for exemple imagine a key children[]. Props Like user, content etc should be sent by the back. For the frontend you can use composition aswell, if you need black/white theme create a that will also be called buy the
@joejoe-lb6bw
@joejoe-lb6bw 4 жыл бұрын
nit: put border on each component so that composition is emphatically visible.
@swyxTV
@swyxTV 4 жыл бұрын
i need a React Training vim course
@someonlinevideos
@someonlinevideos 4 жыл бұрын
Do you mean something about how to best utilize vim while developing React?
@rubenmartinez2807
@rubenmartinez2807 3 жыл бұрын
@@someonlinevideos vim course by React Training ; ] ... btw I do too lol
@RubenGarcia-mr7pe
@RubenGarcia-mr7pe 3 жыл бұрын
He's using `vim` in a very very dumb way, TBH. I use `vim` on a daily basis and I can tell you that I rarely use `hjkl` to move around ;), you see, the real power of `vim` relies on something called `text-objects` (open up vim and type :help text-object to have a better idea), a concrete example, when he removed the `{user}` inside the `WelcomeMessage` component he selected the whole text and, most likely, he pressed `x`, instead, the `vim way` dictates that you do `di(` and that's it!!, what that motion will do is `remove the things inside the parenthesis` :) that's how awesome `vim` is. Anyway, I know this may sound like a rant from a `vim zealot`, IT'S NOT, I've seen both Michael & Ryan using `vim`, they do need to improve! hahaha, but I'm glad that developers like them choose `vim` over something like `VSCode` and they even do that on Live conferences so... I admire both of them and it's very nice that they show off that to other developers and hopefully the jump into the `vim`, `emacs`, whatever.
@NickNijenhuis
@NickNijenhuis 3 жыл бұрын
@@RubenGarcia-mr7pe Why do you like people using VIM over VSCode?
@RubenGarcia-mr7pe
@RubenGarcia-mr7pe 3 жыл бұрын
@@NickNijenhuis I don't tell people what IDE/Text Editor they should use, that's something personal IMHO. I prefer vim/Emacs over something like VSCode, Intellij, etc because I feel very comfortable using them and, from experience and looking at how other people work, I can tell you that I'm 10x faster than them :), that doesn't mean I'm a superb person or anything, it's just that those tools don't require you to be raising your hand to open/close a sidebar to look for a file (shortcuts exist but when using GUI, for some reason, people forget about that).
@ChibiBlasphem
@ChibiBlasphem 4 жыл бұрын
One use case I thought about who makes composition more ineficient than Context is, if WelcomeMessage depends on props which are defined by DashboardContent you could not use composition without using renderProps. Which is leaking internal mechanics of DashboardContent.
@AvindraGoolcharan
@AvindraGoolcharan 4 жыл бұрын
10:02 to skip to context and jump right into composition example
@irasanchez1265
@irasanchez1265 4 жыл бұрын
Thank you
@MobiusCoin
@MobiusCoin 4 жыл бұрын
Super useful! (for others, I've been in this situation, but as I am still getting used to react, all of that context (no pun inteded) was actually really useful)
@MichalionWhite
@MichalionWhite 4 жыл бұрын
thank you
@jonas-mm7em
@jonas-mm7em 2 жыл бұрын
Thank you for the video. It was instructive.
@semicolonsons
@semicolonsons 4 жыл бұрын
Thanks for an interesting video and some fresh thoughts. I subscribed on the strength of it. I agree with your point about not liking the implicitness of context. Correct me if I'm wrong, but the main trade-off I'm seeing with the approach you outlined is that the parent (or grand-parent) component now needs to know more about the shape of its child components, i.e. there is less abstraction and more information leakage across boundaries. The "Law" of Demeter comes to mind en.wikipedia.org/wiki/Law_of_Demeter
@adamtak3128
@adamtak3128 3 жыл бұрын
So simple but it blew my mind. Thanks for this amazing tip. I'm going to go and refactor some code now :)
@firewatermoonsun
@firewatermoonsun 4 жыл бұрын
Thank you. Now I understand what component composition is.
@Pareshbpatel
@Pareshbpatel 2 жыл бұрын
Very informative tutorial on how to avoid Prop-Drilling or Context by using Composition. Thanks very much, Michael {2022-03-14}, {2023-05-25}, {2023-07-22}
@jairovera8065
@jairovera8065 3 жыл бұрын
Great video that shows how to implement composition. I’ve recently started using React for a small work application and I’m loving it. For a small chain of components, prop drilling works fine. But it gets confusing with a larger chain. I’m going to try composition the next time I get into a mess with prop drilling haha. I’d rather see the pros/cons of prop drilling and composition before using Redux in my current application.
@elmoutaouakkilmohammed1432
@elmoutaouakkilmohammed1432 Жыл бұрын
Wow!!! this was very useful, Thank you very much
@YannickL
@YannickL 4 жыл бұрын
Great to change the angle with which we approach things, thanks
@someonlinevideos
@someonlinevideos 4 жыл бұрын
Not sure I agree here fully but gave thumbs up because of the great explanation of your thought process. Also subbed!
@YoannBuzenet
@YoannBuzenet 4 жыл бұрын
Thanks for this tutorial, it was great to discover this pattern.
@constantine7862
@constantine7862 3 жыл бұрын
great video man, very clear and direct to the point
@XavierBrinon
@XavierBrinon 3 жыл бұрын
redirected from epicreactdev, discovered a past me already liked the video.
@user-fc8xh7uo4c
@user-fc8xh7uo4c 11 күн бұрын
Very interesting, thanks!
@jimyquintanillaceron7807
@jimyquintanillaceron7807 2 жыл бұрын
Amazing solution, thank you
@JS_shorts_dev
@JS_shorts_dev 9 ай бұрын
Thanks .. learned something new!
@atefehfakhari8293
@atefehfakhari8293 5 ай бұрын
Great video. Thanks!
@sajadtorkamani9399
@sajadtorkamani9399 2 жыл бұрын
Context is my best friend :)
@codefinity
@codefinity 2 жыл бұрын
I just tried refactoring out some useReducer-useContext stuff to make my components more composable. 👏🏾. I did lose a few components that were kind of pointless and did eliminate the need to create 'global state' via reducer-context. Henceforth, I will stive to think 2x about whether or not said state REALLY is global in nature before I reach for reducer-context (and still will probably never use Redux).
@priyasivakumar3607
@priyasivakumar3607 2 жыл бұрын
Nailed it!
@TheWebCore1
@TheWebCore1 2 жыл бұрын
Thanks for the opinion, you definitely show an alternative pattern for avoiding prop drilling and context, but I’m not a fan how the app file defines the sub-components of dashboard, it makes more sense to call them from a dashboard component which would probably be a separate container file anyway
@2005bgva
@2005bgva 2 жыл бұрын
Thanks, a very good video.
@MrTwangstaable
@MrTwangstaable 3 жыл бұрын
That cleared that up really well for me; thank you :)
@tomerraitz2930
@tomerraitz2930 3 жыл бұрын
Very good explanation! Thank you
@mrjebbar
@mrjebbar 4 жыл бұрын
Fabulous tutorial. Thank you very much, very useful.
@okoiful
@okoiful 4 жыл бұрын
Taking this into consideration, I guess you would reach out for redux even less then for context right?
@amanmavai99
@amanmavai99 3 жыл бұрын
very clearly explained. subscribed.
@seonyeongjang3459
@seonyeongjang3459 2 жыл бұрын
Thanks for explain.
@zacharyshifrel9107
@zacharyshifrel9107 3 жыл бұрын
Super helpful video, thanks!
@jarodsmith9042
@jarodsmith9042 2 жыл бұрын
I think I get where you are coming from. A lot of people are asking about components nested 5, 6, 10 layers deep, and I think there's rarely a need to go beyond 4 layers of abstraction. Atomic component design has 4 layers in it, atoms, molecules, organisms, and pages (also templates). This component design philosophy is rooted in composition. atoms are composed to make molecules, which are composed to make organisms and so on. With proper composition, colocation of state, and thoughtful abstraction you can build any modern web application. In cases where you want to know about the meta state of the app (theme, scroll position, current location, etc...) then context can be helpful because it's already a global state.
@excodex8265
@excodex8265 3 жыл бұрын
Excellent tutorial!
@alexlun4464
@alexlun4464 3 жыл бұрын
Great video. Also, neat Vim skills.
@valenciawalker6498
@valenciawalker6498 9 ай бұрын
Thank you
@MahdiValizadeh
@MahdiValizadeh 2 жыл бұрын
I don't totally agree here, this approach may work for this example but in a real project it will create a huge complexity, imagine a high-level component has to know about the exact detail of a low-level component (e.g: the order of rendering components in the DashboardContent) then if something needs to be changed in DashboardContent your changes would be on AppComponent which doesn't make sense, this approach has put all the other component's responsibility on a high-level components shoulders for the sake of not using context. In another word, you will end up having too much unnecessary code and huge component files that are defining nested components that are totally out of their responsibility. in this example, the AppComponent responsibility is to navigate the user to the correct section of the app based on the authentication status the rest is not its concern but it ends up rendering a welcome message, which violates the separation of concerns principle. also, this component should do one and only one job which is doing more, that is a violation of the single responsibility principle Recap: the objection to components ambiguity is valid but the solution is making a bigger problem.
@williamprada9805
@williamprada9805 Жыл бұрын
Maybe because I am a newbie about all this react workflow. But a lot of comments talking about huge apps. I am a really bad at visual design, but my strong suit is data design. How deep do they need the children on a tree of components? Are you sure that is the right way to go? I would say if it is 7 levels deep in your DOM hierarchy and you still need passing props, watch out!!! And 7 levels is already making my data display itchy. Even on advanced platforms like your own profile administrator on Google or a Spreadsheet software (MS Excel, libreoffice calc), you do not get to 6 levels or more, and if you do is for really specific cases. So I'm on board with this and for me is going to be the way I will be making my react apps, taking into account that I will be using tanstak react query to help me with the cache of server-state.
@TelosMediaOrgOfficial
@TelosMediaOrgOfficial 4 жыл бұрын
It is not useful, it is awesome! Thanks for sharing!
@user-ie7md3mm6x
@user-ie7md3mm6x 2 жыл бұрын
thank you!
@boblitex
@boblitex 3 жыл бұрын
some will argue using context for something this simple is overengineering. I, a simple man do not see it that way. Context for the win
@braydenwerner197
@braydenwerner197 Жыл бұрын
Great video :)
@eidiazcas
@eidiazcas 3 жыл бұрын
the problem with context is its impurity, it makes component reusability harder, and produces unpredictability as you don't have the same output for the same props on a component (because context can change)
@varolo74
@varolo74 9 ай бұрын
Thanks for this video, what if WelcomeMessage needs also some data coming from DashboardCompononent?
@AmanpreetSingh
@AmanpreetSingh 4 жыл бұрын
excellent video! very informative
@greggsworld4453
@greggsworld4453 3 жыл бұрын
This is more a component architecture than an app architecture. If user was being used on multiple pages and you needed to update user in some places, your example here doesn't fly. So i think when pages need to share context and do data manipulation, it's nice to have that in one place in the context. If you want to avoid the issue of components using the context then you let the page read the context and expose the data and methods to your components as you have shown here with composition. That way you have the best of both worlds. Try stay away from using context in components and keep it at page level. Cause then you have a nice overview of what your page is trying to achieve. Overtime your context will get bloated and there's no code splitting on the context, react bundles your entire context into the app.js file whether you are using methods in it or not. So be careful of that.
@maorben3313
@maorben3313 2 жыл бұрын
One Tree to Rule Them All
@asifurrahman5436
@asifurrahman5436 2 жыл бұрын
would love to see when should i use context or redux
@woawh5616
@woawh5616 4 жыл бұрын
Great video, thank you!
@berellevy1
@berellevy1 4 жыл бұрын
wow amazing this is really cool!
@TheBswan
@TheBswan 3 жыл бұрын
You say this pattern addresses the hypothetical problem of accidentally putting a component outside of the context it needs. I fail to see how that is meaningfully different from accidentally composing a component in the wrong place. Especially for a user context which should simply wrap the entire application. The other problem you bring up is what happens if user is undefined? Again I don't see how composition and props actually addresses this problem. If user data has to be retrieved, whether you use context or not has no impact on handling the logic of whether you've found a user. However, I do think this effectively illustrated what component composition looks like and how flexible react is when you learn to use children. Thanks for contributing to the conversation and please explain any disagreements you have with my points!
@Kobayashhi
@Kobayashhi 3 жыл бұрын
Great content man.
@MattDuarte11
@MattDuarte11 4 жыл бұрын
That’s cool. I forgot about composition. There is an issue though, this only works if you have nested components. Unless I’m wrong
@sfyire
@sfyire 4 жыл бұрын
Relying on the discipline of co-workers to use higher order components seems like a bad bet, I'd consider a Fulcro or Om Next pattern to make this a non issue again
@angstrom1058
@angstrom1058 2 ай бұрын
New to React, not new to programming. I think the React best practices include 1) Children first, 2) Composition second, 3) Context third. Global variables should be avoided (common rule to all languages)
@viktorsoroka4510
@viktorsoroka4510 4 жыл бұрын
Makes sense. I am just wondering about the case when more than one prop is really needed for nested components that are placed not near each other (say at the top and at the bottom of the component). In that case, using that mechanism would mean to have more stuff passed to the children which may be too much effort just to get rid of the context.
@mateuszj47
@mateuszj47 4 жыл бұрын
Old good slot pattern. But quite hard to making use of it in complex component compositions.
@mzsrtgzr2
@mzsrtgzr2 4 жыл бұрын
great insights thank you
@lucasfelipe-ze5sy
@lucasfelipe-ze5sy 3 жыл бұрын
Nice video
@laynetaylor7768
@laynetaylor7768 2 жыл бұрын
This is great but also what extension makes your closing tag a different color?!
@chocolatelover3770
@chocolatelover3770 3 жыл бұрын
Correct me if I’m missing something but every time you use Dashboard, you’d need to pass all of it’s children. You’d be rewriting a whole bunch of code (the children) every time which kind of misses the whole point if components? Also, I think there’s more room for error here (forgetting to pass currentUser to a child of a given instance of the Dashboard) than just simply using context or Redux
@cypriankamilnorwid7777
@cypriankamilnorwid7777 2 жыл бұрын
It all depends on the context of usage. If you have a different dashboards this approach that he presents is actually good one, and doesn't miss the whole point of components, on the contrary - it utilizes it. Dashboard may have some different operations inside, like "save to pdf" and it will take the children prop to perform such operation. This is actually really good pattern for some cases. But as always with writing code - you need to think what actually is good approach for your purpose.
@miguelisaac4454
@miguelisaac4454 2 жыл бұрын
thanks, you help this developer be less dumb (^_^)-b I already know the concept, but is easy to fall using context everywere haha
@n3o7oKyO
@n3o7oKyO 3 жыл бұрын
Very nice example :)
@maximodakila2873
@maximodakila2873 4 ай бұрын
May I ask what plugin is that that gives the ending tags a different color? I really like it. Makes the code easier to read.
@ericcartman4799
@ericcartman4799 3 жыл бұрын
Hi, thanks. That was a great demonstration how we can use composition to avoid props drilling. But what about the case when components are mounted of different routes. Say data input by user in '/' route is needed in '/welcome' route component, can we manage this without use of Context.
@mrunal_dhole
@mrunal_dhole 4 жыл бұрын
Thank you.
@justadad134
@justadad134 4 жыл бұрын
Vim's di( might help delete some of those props. :D
@ReactTraining
@ReactTraining 4 жыл бұрын
Yes! I need to use that more often. I've gotten accustomed to using dib which deletes inside the nearest set of braces, but in the case of ({ user }) it would change it to ({}) and leave me with the curlies. I need to not be lazy and reach for the (
@papadelta
@papadelta 4 жыл бұрын
@@ReactTraining didn't know about dib, thanks :D For that case I also use da}. Also, learning some more motions (e, B, W, etc.) would speed you up. I learned a lot playing vimgolf.com
@andrewhaust3382
@andrewhaust3382 4 жыл бұрын
@@ReactTraining You might have a plugin that is causing that behaviour-or possibly accidentally hit shift+b once and never looked back-but out of the box, dib targets () and diB targets {}.
@arthurfedotiew3609
@arthurfedotiew3609 Жыл бұрын
This is so obvious of an approach since HTML has been working like this for ages... Something went wrong in people minds. I wonder though if it is possible to pass down a few children and put each one in particular slot inside jsx of a component children are passed to.
@stevereid636
@stevereid636 4 жыл бұрын
Thanks for sharing! 👍🏾
What is Prop Drilling in React? (And how to prevent it)
12:44
PedroTech
Рет қаралды 22 М.
Can A Seed Grow In Your Nose? 🤔
00:33
Zack D. Films
Рет қаралды 32 МЛН
Get 10 Mega Boxes OR 60 Starr Drops!!
01:39
Brawl Stars
Рет қаралды 8 МЛН
Kids' Guide to Fire Safety: Essential Lessons #shorts
00:34
Fabiosa Animated
Рет қаралды 11 МЛН
Composition Vs Inheritance - Why You Should Stop Using Inheritance
10:16
Web Dev Simplified
Рет қаралды 174 М.
Stop Doing this as a React Developer
12:27
CoderOne
Рет қаралды 162 М.
Goodbye, useEffect - David Khourshid
29:59
BeJS
Рет қаралды 498 М.
Compound Components in React (Design Patterns)
18:21
Cosden Solutions
Рет қаралды 35 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 469 М.
Goodbye, useEffect: David Khourshid
26:01
Real World React
Рет қаралды 233 М.
Component Composition - React In Depth
22:43
Tech with Nader
Рет қаралды 3,7 М.
Preventing React re-renders with composition
12:11
Developer Way
Рет қаралды 18 М.
3 Ways to Avoid Prop Drilling in Vue/React
9:34
Lachlan Miller
Рет қаралды 4,5 М.
Can A Seed Grow In Your Nose? 🤔
00:33
Zack D. Films
Рет қаралды 32 МЛН