如何防止平面列表页眉或页脚在反应中重新呈现

Sha*_*kim 8 footer rerender react-native react-native-flatlist

我在 flatlist 的页脚中放置了一个输入字段,但是当我尝试输入任何内容时,它会自动关闭键盘,因为重新渲染了 flatlist 页脚。

我试图从 Scrollview 嵌套 flatlist 但这带来了警告..

如何阻止页脚被重新渲染?我可以在不嵌套 Scrollview 的 flatlist 的情况下解决这个问题吗?

<FlatList
              ListHeaderComponent={() => (
                <View style={styles.discountContainer}>
                  <Text style={[styles.buttonText, { letterSpacing: 3 }]}>
                    10% DISCOUNT ON 8 COURSES
                  </Text>
                </View>
              )}
              numColumns={2}
              data={data}
              renderItem={({ item }) => (
                <View>
                  <SingleProduct item={item} />
                </View>
              )}
              ListFooterComponent={() => (
                <View>
                  <View style={styles.couponContainer}>
                    <Input
                      placeholder='Coupon code'
                      placeholderTextColor='#0a5796'
                      color='#0a5796'
                      inputStyle={{
                        color: '#0a5796',
                      }}
                      inputContainerStyle={{
                        borderBottomWidth: 0,
                        height: 50,
                      }}
                      containerStyle={styles.couponInputContainer}
                      onChangeText={(value) =>
                        this.setState({ couponCode: value })
                      }
                      value={this.state.couponCode}
                    />
                    {couponLoading ? (
                      <View style={styles.couponButton}>
                        <ActivityIndicator />
                      </View>
                    ) : (
                      <TouchableOpacity
                        style={styles.couponButton}
                        onPress={() => this.codeCheck(couponCode, line_items)}
                      >
                        <Text style={styles.buttonText}>Apply Coupon</Text>
                      </TouchableOpacity>
                    )}
                  </View>
                </View>
              )}
            />
Run Code Online (Sandbox Code Playgroud)

sut*_*her 19

Arrow-Funktions“总是”执行并在内存中创建一个新的引用。这样,如果组件将被执行,它们将始终重新渲染。

出于性能原因,您最好在外部定义您的函数并像这样调用它:

function renderMyItem(){  ...bimbom... yous stuff goes here! }
function renderHeader(){  ...bimbom... yous stuff goes here! }

<Flatlist
  renderItem={this.renderMyItem()}
  ListHeaderComponent={this.renderHeader()}
  ...
/>

Run Code Online (Sandbox Code Playgroud)

这里发生了什么?如果您的组件已加载并将保存在内存中renderMyItemrenderHeader则您的两个功能将被执行一次。因此,每次调用其中一个函数时,都会调用对内存中保存它们的位置的引用。

在另一种情况下,Arrow-Functions()=>{...}在当前上下文中执行,并在每次调用时在 Memory 中生成一个新的引用,因为 .. 说清楚:你以这种方式定义调用一个函数。


小智 17

如果您使用的是功能组件,则不要() => (...)对 FlatList 的页眉和页脚组件使用箭头函数,而是仅返回页眉和页脚组件,如下面的示例所示。

<FlatList
    ...
    ListHeaderComponent={(<View><Text>Header</Text></View>)}
    ListFooterComponent={(<View><Text>Footer<Text></View>)}
/>
Run Code Online (Sandbox Code Playgroud)