How to create and connect a custom component theme for Native Base
Package versions:
I'm using Native Base 2.0+, the themes are ejected and using StyleProvider I am able to tweak and customize any Native Base component according to my theme, no problem.
Following the docs, it's stated that to add themes to a custom component we should define a namespace for said component and merge it with the instantiated styling as well. Code example below:
import React, { Component } from 'react'
import { Header, Left, Body, Right, Button, Title, Text, connectStyle } from 'native-base'
import Colors from '../Themes/Colors'
import ApplicationStyles from '../Themes/ApplicationStyles'
class NBHeader extends Component {
render () {
const styles = this.props.style
return (
<Header style={styles.header}>
<Left>
<Button transparent>
<Text style={styles.headerBackButton}>
{'< Back'}
</Text>
</Button>
</Left>
<Body>
<Title>Login</Title>
</Body>
<Right />
</Header>
)
}
}
export default connectStyle('CustomComponents.Header', ApplicationStyles)(NBHeader)
In this case, namespace for the component is 'CustomComponents.Header'. Then, we use StyleProvider to connect the Theme:
import React, { Component } from 'react';
import { StyleProvider } from 'native-base';
class CustomComponent extends Component {
render() {
return (
// connect styles to props.style defined by the theme
const styles = this.props.style;
<StyleProvider style={customTheme}>
Your Custom Components
</StyleProvider>
);
}
}
// Define your own Custom theme
const customTheme = {
'yourTheme.CustomComponent': {
// overrides CustomComponent style...
}
};
Since I've ejected the theme, I entered the new namespace for the Custom Component in NB's Theme file, so it should already be added and cascaded using StyleProvider.
So for instance, if I want the header to be 'red' and have a padding of '10' due to theming rules, I add those as default props of 'CustomComponents.Header' and forget about it, it will always be applied to the component as long as the StyleProvider is cascading themes.
The problem is I cannot get this defined Custom Component's default theme to be applied. I don't know if it's a problem with my code or how Native Base works. Any help would be appreciated and I can provide further code if needed.
This issue is 26 days old and no response. Is there any way to get help here?
Hey @Nahueleti, Sorry that it took so long.
I tried replicating this issue, trying to understand your problem.
From your first code snippet, you have imported custom styles for your custom component as
ApplicationStyles from '../Themes/ApplicationStyles'
I assume that is where you have defined all Default styles for your CustomComponent.
I have done something similar, just that, i defined the default styles in the CustomComponent itself and applied them as ApplicationsStyles in my Component, as shown below.
import React, { Component } from "react";
import { View, Text, StyleSheet } from "react-native";
import { connectStyle } from "native-base";
class MyCustomComponent extends Component {
render() {
// connect styles to props.style defined by the theme
const styles = this.props.style;
return (
<View style={styles.container}>
<Text style={styles.textContent}>
Your Component with static style
</Text>
</View>
);
}
}
// My Custom styles here
const styles = {
container: {
flex: 1,
backgroundColor: "green"
},
textContent: {
fontSize: 20,
color: "red"
}
};
// connect the component to the theme
export default connectStyle("namespace.myComp", styles)( // <= custom styles object applied
MyCustomComponent
);
Now, all these default styles will retain whenever i use this CustomComponent.
If i want to over-ride, some of these styles, i can do that using StyleProvider.
Something like this
import { StyleProvider } from "native-base";
export default class App extends React.Component {
render() {
const styles = this.props.style;
return (
<StyleProvider style={customTheme}>
<CustomComponent />
</StyleProvider>
);
}
}
const customTheme = {
"yourTheme.CustomComponent": {
textContent: {
fontSize: 40,
color: "yellow"
}
}
};
This will over-ride default style of CustomComponent with new styles i just provided. So, please check if you have imported the correct styles or try applying the method above and let us know if it works for you.
Closing this due to inactivity