I am new to React Native and I am facing a big challenge with the usage of gradients.
My goal is to create a component library, that is going to be reused in other React Native applications and some of my components are styled with gradients. For instance, the primary Button component has gradient background.
Some famous solutions are react-native-linear-gradient and Expo Linear Gradient, but the problem is that neither of them seems like a generic solution for a component library:
react-native-linear-gradient, it does not run in Expo projects (the documentation for my components is based on Expo). It breaks with Invariant Violation: requireNativeComponent: "BVLinearGradient" was not found in the UIManager.. I need to say that I researched about this issue and nothing worked - I even ejected the Expo configuration, installed pods, etc. but the issue persists.Is it generally possible to create such library of components with gradient styles that run seamlessly on various React Native environments?
I use webpack for building my library, if this can give more detail to the question.
The solution I found for this issue was to make my Button receive the platform specific gradient component as prop. The styling approach for both above-mentioned libraries is the same, so this worked great for my case:
{/* LinearGradient is either Expo gradient or react-native-linear-gradient */}
<Button GradientComponent={LinearGradient}>
...
</Button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With