I'm working on a React Native app. I can get the results I want by wrapping every single one of my container components in something like this:
<LinearGradient
style={{ flex: 1 }}
colors={[primary, primaryGradient2, primaryGradient1]}
locations={locations}
>
<ContentHere></ContentHere>
</LinearGradient>
What I would like to do is keep my project DRY and not have to repeat doing this for every container level component.
I tried making a component that returns the LinearGradient with the proper config I'd like based off a theme object, something like this:
<GradientTheme theme={theme}>
<MainContainer>Stuff here </MainContainer>
</ GradientTheme>
However, this renders without any of the MainContainer elements at all. The code for GradientTheme is as follows:
import React from 'react';
import PropTypes from 'prop-types';
import LinearGradient from 'react-native-linear-gradient';
export const GradientTheme = ({ theme }) => {
const { primary, primaryGradient2, primaryGradient1, locations } =
theme;
return (
<LinearGradient
style={{ flex: 1 }}
colors={[primary, primaryGradient2, primaryGradient1]}
locations={locations}
/>
);
};
GradientTheme.propTypes = {
theme: PropTypes.object
};
Just some extra info, I'm also using React-Navigation. A more ideal solution would be having a global way to apply all of this styling and theming.