使用 TaroJS 的一些问题记录

2020-12-19  本文已影响0人  莫帆海氵

以下文档记录使用于 tarojs@2.x

Taro 优化目标

Taro总结

避免在 map 循环中引用的组件传入计算值

<View className="content">
    {items.length > 0 &&
        items.map((item, i) => (
            <BrickGoodItem good={item} key={i} isStart={nowTime >= conf.start} posid={posid} />
    ))}
</View>

会生成以下的数据

var loopArray0 = items.length > 0 ? items.map(function (item, i) {
        item = {
          $original: (0, _index.internal_get_original)(item)
        };
        var $loopState__temp5 = items.length > 0 ? nowTime >= conf.start : null;
        return {
          $loopState__temp5: $loopState__temp5,
          $original: item.$original
        };
      }) : [];
      Object.assign(this.__state, {
        loopArray0: loopArray0,
        posid: posid,
        items: items
      });
      return this.__state;

这样会使传递给 setData 的数据包含重复 items 和 loopArray0

避免在 render 中使用条件返回

if (condition) {
    return <View/>;
}

Taro 会在createData 方法中生成

if (condition) {
    return null;
}

在以下的 dataDiff 计算更新组件的时候,createData 返回 null 则保持不变,没有变化就不会更新对应组件,可能使得组件还是使用上次 setData 的数据,造成显示错误
if (component._createData) {
   // 返回null或undefined则保持不变
   data = component._createData(state, props) || data;
}

应当使用条件表达式 condition && <View/>

上一篇 下一篇

猜你喜欢

热点阅读