How to upgrade React-Native Modules in a backwards compatible manner

Emelia Smith
1 min readJan 24, 2017

--

Between v0.39 and v0.40 of React-Native, they made a major breaking change; What changed were the import paths for the objective-c modules that native modules use. Instead of doing #import "RCTConvert.h" you’d now need to do #import "React/RCTConvert.h" — and they didn’t share any upgrade guide.

This has caused a huge fragmentation in the ecosystem: you can either upgrade to v0.40, or you can’t.

I asked on twitter a little while ago about whether it was possible to support both the versions, and all that I was able to find was a pull request that was rejected, which could’ve prevented this major breaking change.

However, I just found a very nice little gem in the react-native-code-push repository. This is that there’s a macro in the clang compiler called __has_include which enables you to test whether a given header can be included.

The backwards compatible upgrade path for react-native modules is to check which file we can import, rather than just swapping out the paths directly. Doing so looks something like the following:

Which tests first whether it can include the header from the old path, then tries the new path.

This means that we can all update our react-native modules without forcing breaking changes on users.

--

--

Emelia Smith

Founder of Unobvious Technology UG, survivor of startups, tech princess. You probably use or benefit from my code.