Day 20 To Do List - 封装

第 20 天~

走到了三分之二了~!!!

好激动阿!!

再接再厉~ 加油!

昨天把 To Do List 的逻辑全都完成了,

我们今天来 聊聊 关於 compound component (合成元件)

当我们在开发时,总会碰上几个状况:

  • 有相同地方,重复性高
  • 不同逻辑的干扰
  • 结构太多难以阅读

在这种时候,我们会希望能把这些独立出来,封装成一个 component ,

To Do List 为例的话,我们会拆分成哪些 component 呢?

功能按钮

Add & Search & Complete All 这三个按钮,不管是功能还是外型都是一样的,

那我们就可以把它封装成 FeatureButton

功能 :

  • onPress 按下去事件
  • text 显示按钮文字
const styles = StyleSheet.create({
  root: {
    flex: 1,
    backgroundColor: '#d9d9d9',
    borderColor: '#7c7c7c',
    borderWidth: 1,
    padding: 5,
  },
  text: {
    textAlign: 'center',
    color: '#555',
  },
});

function FeatureButton({ text, onPress, ...etc }) {
  return (
    <TouchableOpacity style={styles.root} onPress={onPress} {...etc}>
      <Text style={styles.text}>{text}</Text>
    </TouchableOpacity>
  );
}

因为内部没有独立逻辑,我们可以只需要使用 functional component 就好,

藉由 props 把所需的资讯传入,

列表 item

在 FlatList 的 renderItem 部分,

我们也可以把它封装成一个 component 来增加阅读性,

因为封装起来,我们有些地方需要做改变,

  • 传入目前是奇数还是偶数
  • 传入目前是否是结束状态
const styles = StyleSheet.create({
  root: {
    paddingVertical: 17,
    flexDirection: 'row',
  },
  odd: {
    backgroundColor: '#eee',
  },
  even: {
    backgroundColor: '#f9f9f9',
  },
  doneBackground: {
    backgroundColor: '#888',
  },
  text: {
    marginLeft: 10,
  },
  doneText: {
    color: '#fff',
    textDecorationLine: 'line-through',
  },
  tickArea: {
    marginHorizontal: 10,
    transform: [{ rotate: '45deg' }],
    height: 14,
    width: 8,
  },
  tick: {
    borderBottomColor: '#fff',
    borderRightColor: '#fff',
    borderBottomWidth: 1,
    borderRightWidth: 1,
  },
});

function ToDoItem({ text, isDone, isEven, onPress }) {
  const backgroundColorStyle = isEven ? styles.even : styles.odd;
  return (
    <TouchableOpacity
      style={[
        styles.root,
        backgroundColorStyle,
        isDone && styles.doneBackground,
      ]}
      onPress={onPress}>
      <View style={[styles.tickArea, isDone && styles.tick]}></View>
      <Text style={[styles.text, isDone && styles.doneText]}>{text}</Text>
    </TouchableOpacity>
  );
}

那外部使用就是这样:

<FlatList
  data={list.filter((item) => item.text.includes(filterKey))}
  renderItem={({ item, index, separators }) => {
    const isEven = index % 2 === 0;
    const isDone = item.status === 'done';
    return (
      <ToDoItem
        isEven={isEven}
        isDone={isDone}
        text={item.text}
        onPress={this.changeItemStatus(item.id)}
      />
    );
  }}
  keyExtractor={(item) => item.id}
/>

<<:  初学者跪着学JavaScript Day20 : 原型毕露(中)

>>:  30天学会C语言: Day 19-考试常用的输入格式

《赖田捕手:追加篇》第 31 天:初始化 LINE BOT on Heroku

第 31 天:初始化 LINE BOT on Heroku 事实说来可笑:别试着和任何人讨论任何关於...

Snowflake SnowPro-Core Dumps - The Best Option To Pass Exam

Use Actual Snowflake SnowPro-Core Dumps to Learn F...

【D12】制作图表:加权指数和交易金额的图表

前言 进行观察还是需要用图表表示,这时候叠图就很好用。之前都一张一张画,现在呈现叠图的方式。 本日程...

大脑如何精准学习 (1) 注意力

今天,产生了一个想法、或说好奇:是否有什麽原因,让原本在学习表现上有开放可能性的人,变成不主动积极了...

Day21-pytorch(4)Dataset、DataLoader

为什麽需要用到这两个东西呢? 因为我们在训练资料时 如果每次输入的资料都是一整个一样的资料,表示每次...