As @Mentor pointed out in the comments:
.create
still only validates in development and does nothing else. In production it just returns the object. See source code in repository.
source code
create<+S: ____Styles_Internal>(obj: S): $ObjMap<S, (Object) => any> {
if (__DEV__) {
for (const key in obj) {
StyleSheetValidation.validateStyle(key, obj);
if (obj[key]) {
Object.freeze(obj[key]);
}
}
}
return obj;
}
I think this comment deserves to be more noticeable. So I post it as an answer.
Additionally, I'd like to point out that validation - is a good thing, but there is another, better way to validate - Typescript:
const styles = StyleSheet.create({
someViewStyle: { ... },
someTextStyle: { ... },
})
can be replaced with
import { ..., ViewStyle, TextStyle } from 'react-native';
interface Styles {
someViewStyle: ViewStyle,
someTextStyle: TextStyle,
}
const styles = {
someViewStyle: { ... },
someTextStyle: { ... },
}
And it's not just static-time check, it also allows to discriminate between ViewStyle
and TextStyle
.
But there are more lines of code. So, personally, I prefer to go without styles
object, if possible:
const someViewStyle: ViewStyle = { ... },
const someTextStyle: TextStyle = { ... },