Reputation: 11
I was wondering about good patterns concerning the react context.
Let's get a public library, for example react-intl
that exposes a Provider (IntlProvider).
Let's create a library barLib
that uses react-intl
to manage translations internally and that also exposes a Provider
.
Now let's imagine I create a web app and use both barLib
and react-intl
.
I don't expect the barLib
to ever override my react-intl context, because i'm not aware it uses internally the react-intl
lib.
But the barLib
can accidentally override the react-intl context => https://codesandbox.io/s/embedded-contexts-test-z8e7b
This kinda breaks the isolation of libs IMO.
I see several solutions :
barLib
(pretty extreme)barLib
, if so merge the context values ?I'm not convinced with either solutions, what do you guys think about it ?
Upvotes: 1
Views: 120
Reputation: 11
After giving some more thoughts about this. I think you should mark the react-intl
library as a peer dependency in the barLib
and document the fact that to use the barLib, you need to wrap it in a react-intl
context.
In conclusion, if you want to use a context in your library, just make it a peer dependency and don't put the Provider in it
Upvotes: 0