
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-native-themes
Advanced tools
$ npm install react-native-themes --save
OR
$ yarn add react-native-themes --save
//colors.js
import Theme from 'react-native-themes';
const colors = new Theme.Colors({
DEFAULT_THEME: {
backgroundColor: '#fff',
},
DARK_THEME: {
backgroundColor: '#3b5998',
}
});
export default colors;
Note - Do not forget to import this file at the top of your component(ie. App.js).
By default the first theme in the colors template is selected. In the root component of your app like App.js, register ThemeListeners for the component to act on the default theme applied.
//App.js
componentWillMount() {
this.listener = Theme.addThemeListener(() => {
this.setState({});
});
}
componentWillUnmount() {
Theme.removeThemeListener(this.listener);
}
Create the stylesheet for your components as a function.
Example -
import { StyleSheet } from 'react-native';
const styles = (colors) => {
return StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: colors.backgroundColor,
},
...
});
}
export default styles;
In the components where you would like to apply the theme colors call the getStyles(stylesheet) function in componentWillMount().
import Theme from 'react-native-themes';
import stylesheet from './styles'; //Stylesheet created in Step 2
let styles;
class MyComponent extends Component {
...
componentWillMount(){
styles = Theme.getStyles(stylesheet);
//use the styles for styling the components in render methods.
}
...
}
Note
For inline styles, you can get the colors by calling Theme.getColors()
Example
render(){
const colors = Theme.getColors();
return (
<View style={{ backgroundColor: colors.backgroundColor, flex:1 }}>
...
</View>
);
}
By default the first color theme will be applied. If you want to change the color theme you need to call the Theme.setColorTheme(themeName, callback)
themeName - Name of the theme defined in Step 1.callback - Callback function to be called after theme is setExample-
import Theme from 'react-native-themes';
...
render(){
//have to fetch styles again as the Theme Change
//will re-render the Component.
styles = Theme.getStyles(stylesheet);
return (
...
<Button
onPress={() => {
if (Theme.getColorTheme() === 'DEFAULT_THEME') {
Theme.setColorTheme('DARK_THEME', () => {
this.setState({}); //force re-render of the component
});
} else {
Theme.setColorTheme('DEFAULT_THEME', () => {
this.setState({}); //force re-render of the component
}));
}
}}
title="Change Theme" />
...
);
}
Bugs? Comments? Features? PRs and Issues happily welcomed!
Sandeep Tiwari
FAQs
Library that allows you to integrate multiple themes
The npm package react-native-themes receives a total of 21 weekly downloads. As such, react-native-themes popularity was classified as not popular.
We found that react-native-themes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.