wechat-mini-program-experience
Taro 的坑
- 自定义组件的 props 不能传入复杂的渲染函数
// 比如一个带有头像的电话列表 const tels = [ { FIRST_CHAR: 'A' list: [{ avatar: 'imgsrc', tel: '12345678900' },{ avatar: 'imgsrc', tel: '12345678900' },{ avatar: 'imgsrc', tel: '12345678900' },{ avatar: 'imgsrc', tel: '12345678900' }] } ]; // 这样的一个数据结构,因为想做一个通用性更大的组件,所以在设计的时候想要把第二层渲染list的渲染函数写成一个从外部传入的函数 const render = () => { return (<Indexes renderItem={ item => (<View><Image src={item.avatar} /><Text>{item.tel}</Text></View>) } />); } // 但是并不能这样子写,taro不会进行渲染,所以只能够将列表的渲染方法写在组件内部 list.map((listItem) => ( <View key={`index-${listItem.key}`} className="index-list-item" id={`index-${listItem.key}`} > <View className="item-title">{listItem.title}</View> // {renderItem()} 如果是使用上面那种方式 直接在这里写成这样就可以了 {listItem.items.map((i) => { return ( <View key={i.name} className="item-container" hoverClass="item-container-hover" onClick={() => onClick(i)} > {needIcon && ( // 因为不确定 icon 的确切的值,所以就交给 CustomImage 组件内部处理,外面只需要管需不需要icon的属性 <CustomImage src={i.icon} mode="aspectFit" custom-class="icon" defaultImage={needIcon && defaultIcon} /> )} <Text>{i.name}</Text> </View> ); })} </View> // 所以复杂的渲染还是需要写在组件内部 // 另外如果需要传入 tsx 作为 props 的话,需要使用一个 View包裹起来 <Component tsx={<View>something</View>} />
在开发中需要保持的一些思维
小程序中的一些注意事项
埋点小技巧
Last updated