Use React npm library that uses Context API in a pre-hooks version of React

145 views Asked by At

I created and published an npm library. The library uses React.createContext().

I then created a demo react App using (an older, on purpose) react version 16.0.0 and react-dom version 16.0.0

When I run the application i get the error described here: How to use new Feature Hooks in React?

Given that I own the library, is there a way to build/compile it in such a way that it's backwards compatible with older pre-context-api versions of react? I'd like this new npm library to be compatible with older react versions.

Thanks

1

There are 1 answers

0
trixn On BEST ANSWER

It is not possible. A react application can only use a single version of react and that is the one that you use in your project. Libraries for react should only have react as a peer dependency. That means the project using it is responsible for providing the minimal version. You can't and shouldn't bundle react in a library that is intended to be used by other projects, especially not a different version.

So in other words: If you want to use the library in your project, you need to upgrade react to ≥16.3.0.