4 ms·
What's your programming background? This is some quite basic stuff to be honest. Have you heard of dependency injection? Context is dependency injection in Reac
by eliseumds 5y ago
What's your programming background? This is some quite basic stuff to be honest. Have you heard of dependency injection? Context is dependency injection in React, there you go. There's quite a few .Net/Java books that would offer you better examples and clearer reasoning than myself, like this one: https://www.manning.com/books/dependency-injection-principles-practices-patterns https://www.manning.com/books/dependency-injection-principle....
- activitypea 5y agoDon't talk down to people, even when you doubt them. It's not a good look :) Context is an application of the principles behind DI, sure, but that doesn't really address my point.
- eliseumds 5y agoSorry if you felt talked down. I am lucky to have worked in some very big and complex React applications and will share more experience with you: * By using a singleton you're not able to easily test different LocaleData contexts in parallel. * By using a singleton you're not able to make your NodeJS server deal with users with different locale settings because you have only one global instance. Multiple requests arriving at the same time would corrupt each other. You could spawn a separate process for each locale but that's inconvenient. Does that make sense?
- zarathustreal 5y agoDependency injection in modern programming is known as "partial function application" and the poster you're replying to is correct. React Context is not at all an elegant solution, it's the opposite. It's a hard coupling to hidden global state.
- eliseumds 5y agoIt's not about loose or tight coupling, it's about things that React Context enables you do to. You're literally also hiding contextual data when using a partially-applied singleton, and you end up with code that is not safe when writing an event-looped single-threaded application (any NodeJS server implementation). Users cannot use react-helmet when doing stream-rendering, for example, because it's a singleton. See https://news.ycombinator.com/item?id=27032394 https://news.ycombinator.com/item?id=27032394.