我想问社区关于 React Native 中 StyleSheet.create 的变化。
前:
我已经回顾了过去关于这个主题的问题,比如这个问题,但它们都在很久以前得到了回答(除了这个答案,但我想有一些明确的东西),而且从那以后发生了很多变化。
在 StyleSheet 为样式创建唯一 id 之前,主要用于性能优化。如果您想从创建的样式对象中获取样式,您应该使用flatten方法。大多数答案都引用了这个 flatten 方法,您无法像访问普通对象一样访问样式属性。
例如
const styles = StyleSheet.create({
modalContainer: {
width: 100,
backgroundColor: 'white',
padding: 5,
},
Run Code Online (Sandbox Code Playgroud)
您无法访问填充样式,例如styles.modalContainer.padding;
目前:
然而,这种行为已经改变。这是React Native 团队的 StyleSheet 源代码。只需复制 create 方法:
create<+S: ____Styles_Internal>(obj: S): $ObjMap<S, (Object) => any> {
// TODO: This should return S as the return type. But first,
// we need to codemod all the callsites that …Run Code Online (Sandbox Code Playgroud) react-native ×1