Reputation: 1
Is It possible to ad a global variable on a stylesheet Something like this :
const styles = StyleSheet.create({
bigblue: {
color: 'blue',
fontWeight: 'bold',
fontSize: ´$myglobalvar’
},
red: {
color: 'red'
},
});
Thanks
Upvotes: 0
Views: 47
Reputation: 426
Yes, this is possible and a recommended pattern to have a consistent design in your app.
import React, {Component} from 'react';
import {
StyleSheet,
Text
} from 'react-native';
const defaultFontSize = 16;
class MyTextComponent extends Component {
render(){
return (
<Text style={styles.bigblue}>Test</Text>
)
}
}
const styles = StyleSheet.create({
bigblue: {
color: 'blue',
fontWeight: 'bold',
fontSize: defaultFontSize
}
});
DesignConstants.js
const DesignConstants = {
"DEFAULT_FONT_SIZE": 16
}
module.exports = DesignConstants;
MyTextComponent.js
import React, {Component} from 'react';
import {
StyleSheet,
Text
} from 'react-native';
import DesignConstants from './DesignConstants'
class MyTextComponent extends Component {
render(){
return (
<Text style={styles.bigblue}>Test</Text>
)
}
}
const styles = StyleSheet.create({
bigblue: {
color: 'blue',
fontWeight: 'bold',
fontSize: DesignConstants.DEFAULT_FONT_SIZE
}
});
If your app gets bigger, I would definitely recommend the second possibility.
Upvotes: 1