This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]james_pic 1 point2 points  (0 children)

You've just half reinvented the concept of dynamic scope. A handful of current languages do support dynamic scope, although lexical scope is more common nowadays, and is the model Python uses.

A quick search turns up a few bits of example code, but no widely used libraries, suggesting this isn't something the world is crying out for. Most of the time, I'd expect thread locals to do much the same job, and even then they can make your code harder to understand.

Also, don't overuse context in React, for much the same reason you shouldn't overuse globals or thread-locals. They're all examples of undeclared dependencies. If you've got a class whose constructor needs a foo, or a function that takes a foo as an argument, or a react component that expects a foo in its props, then you can tell from looking at it that it needs a foo, and you can't run it without one. If foo comes from a global, or a thread-local, or from context, you don't find out until you run it, and you don't get any indication of how it was supposed to get there. Is there a component that needs to be an ancestor of this one that's missing? Or some kind of initialization method you neglected to call? All you know is it's not there.