Tom Pradat
Tom Pradat

Reputation: 11

React lib overriding your web app context

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 :

I'm not convinced with either solutions, what do you guys think about it ?

Upvotes: 1

Views: 120

Answers (1)

Tom Pradat
Tom Pradat

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

Related Questions