Is this really bad code though? I haven't worked with React for a year or so but I remember a lot of apps where these kind of providers were needed and they were nested like this. Usually ended up cleaning up App.jsx by refactoring them all into OneProviderToRuleThemAll.jsx (may or may not have been the actual name)
Providers are not typically used for state management. The values in these providers do not (or should not) ever change. They provide values that are static to all components beneath them, things like styles.
If someone was actually using this for state management, though, god help them.
Yep, this is all I see here. React is pretty good in most ways but some parts are a very clearly a case of someone, somewhere having a little idea and then everyone adopts with naive fervour.
Nested providers like this image are blatantly bad design. Yes some folk have said there are workarounds and alternative methods, but the simple truth is that loads of React apps do indeed perform some form of nesting like this as the norm.
Yeah, I've had some bad experiences with React too; used it in a pretty basic sense for a college project, and although I get what they're trying to do with reusable components, the useState letting you change what shows up in a list of items without having to repeatedly destroy and recreate them manually, etc, it was such a confusing PITA (never figured out how to fix an issue where trying to pre-populate editable fields in an "edit records" page wasn't working) that I preferred working with vanilla JS.
I whipped up an event bus hook that lets components subscribe to and publish events. It's SO much cleaner and more straightforward than redux or useContext
What's wrong with it? I ask because I'm actually taking a react Udemy course right now at work because I'll be inheriting a project and it teaches redux.
It would have rerendered if it wasn't a provider. The context api makes sure that there aren't any unnecessary rerenders, and only those components are recalculated which consume the changed provider. So no, the entire app won't rerender if one of the provider's state is changed.
When you have a lot of nested providers like this, notice how the order of the nesting does not matter. And if the order does not matter, perhaps it should not have been expressed as nesting to begin with?
This design is really awesome to work with, when coming from react context. But, the only caveat I found in this is that you won't know for sure where the value is getting inject from, just by reading the child component. It could be in the immediate parent or all the way at the top of the tree, you'll have to find it
169
u/Voltra_Neo Dec 03 '22
React libraries be like: slaps roof of App.jsx you can fit so many providers in this bad boi
50
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 03 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
79
u/genericindividual69 Dec 03 '22
Is this really bad code though? I haven't worked with React for a year or so but I remember a lot of apps where these kind of providers were needed and they were nested like this. Usually ended up cleaning up App.jsx by refactoring them all into OneProviderToRuleThemAll.jsx (may or may not have been the actual name)
11
u/FrezoreR Dec 03 '22
I heard you like providers ( ͡° ͜ʖ ͡°)
11
u/nonsensicalnarwhal Dec 04 '22
Sometimes you just need lots of providers 🤷🏻♂️. My work calls this
ProviderSoup28
u/geon Dec 03 '22
When you have a lot of nested providers like this, notice how the order of the nesting does not matter. And if the order does not matter, perhaps it should not have been expressed as nesting to begin with?
5
u/dumbasPL Dec 04 '22
https://vuejs.org/guide/components/provide-inject.html
You're welcome. 😎
Yes, I know these meme is about react
9
u/original_evanator Dec 04 '22
I’ll save some of you the google:
5
u/pyker404 Dec 04 '22
Why i hate react part one
2
u/Cervarl_ Dec 03 '22
Literally me
2
u/micro-amnesia Dec 03 '22 edited Dec 04 '22
I hate this. I hate everything about this.
(Edit: Typo)
2
u/I_AM_THE_S_IN_IOT Dec 03 '22
I bet you guys soon the js people will discover factories
1
u/Objective-Blood8997 Dec 04 '22
🥹🗿✊️
1
u/-Konrad- Dec 04 '22
I've seen this before and it seemed "normal"
1
u/patryky Dec 04 '22
Maybe it is time to write a component that will take providers and nest all of them
-1
u/Gaming_over_sleep Dec 03 '22
I’m really scared