drcmda / react-contextual

🚀 react-contextual is a small (less than 1KB) helper around React 16s new context api
MIT License
642 stars 22 forks source link

Helpers for creating context providers ... #3

Closed drcmda closed 6 years ago

drcmda commented 6 years ago

Under some circumstances Reacts default api can be a little cumbersome for context creation, especially due to the singleton pattern whereas the old api was component bound. There's also the open question as to how you would pass a context down to consumers. They seem to have let this deliberately open in order to see which patterns emerge.

I tried for a couple of days to come up with something more comfortable in order to make it easier to

a. create providers b. refer to them from a consumers perspective c. re-use providers in nested trees with unique identifiers

I came to the conclusion that it's probably best to offer both a hoc and imperative functions for creating/getting/removing a named context. I'm adding this as an issue as a space to collect ideas, suggestions or contributions.

Imperative API

Example: https://codesandbox.io/s/30ql1rxzlq

imperative

HOC and subscribe serving dynamic context

Example: https://codesandbox.io/s/m7q5z407p9

example-7