StyleSheet.create làm gì và tại sao nó lại hữu ích?

{{FormatNumbertoThousand(model.total_like)}} lượt thích
190 lượt xem

StyleSheet là một trừu tượng (abstraction) tương tự như CSS StyleSheets. StyleSheet.create tạo tham chiếu kiểu StyleSheet từ đối tượng đã cho.

const styles = StyleSheet.create({
   container: {
      borderRadius: 4,
      borderWidth: 0.5,
      borderColor: '#d6d7da',
   },
   title: {
      fontSize: 19,
      fontWeight: 'bold',
   },
   activeTitle: {
      color: 'red',
   },
});

Sử dụng StyleSheet:

<View style={styles.container}>
   <Text style={[styles.title, this.props.isActive && styles.activeTitle]} />
</View>

StyleSheet rất hữu ích vì:

  • Bằng cách di chuyển các style khỏi hàm render, bạn sẽ làm cho mã dễ hiểu hơn.
  • Đặt tên cho các style là một cách hay để thêm ý nghĩa cho các component cấp thấp trong hàm render.
{{login.error}}