dragonwong / blog

a blog based on github page
dragonwong.github.io/blog/
9 stars 4 forks source link

react-native 中样式的骚写法 #30

Open dragonwong opened 5 years ago

dragonwong commented 5 years ago

今天在读人家源码时发现了一个rn样式的骚写法:

import {
  StyleSheet,
  Platform,
} from 'react-native';

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#eee',

    ...Platform.select({
      ios: {
        shadowColor: 'rgba(0,0,0,0.2)',
        shadowOpacity: 1,
        shadowOffset: {
          height: 2,
          width: 2,
        },
        shadowRadius: 2,
      },

      android: {
        elevation: 0,
        marginHorizontal: 30,
      },
    }),
  },
});

以上代码有两个骚处:

  1. 通过 Platform.select() 这个骚写法可以很方便地做平台适配;
  2. elevation 这个是安卓下的阴影实现