如何防止平面列表页眉或页脚在 React Native 中重新呈现
How to prevent flatlist header or footer from re-render in reactnative
我在 flatlist 的页脚中放置了一个输入字段,但是当我尝试键入任何内容时,由于重新呈现 flatlist 页脚,它会自动关闭键盘..
我试图从 Scrollview 嵌套平面列表,但这带来了警告..
如何阻止重新呈现页脚?我可以在不嵌套 Scrollview 的平面列表的情况下解决这个问题吗?
<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>
)}
/>
Arrow-Funktions“总是”执行并在内存中创建一个新的引用。这样,如果组件将被执行,它们将始终重新渲染。
出于性能原因,您最好在外部定义您的函数并像这样调用它:
function renderMyItem(){ ...bimbom... yous stuff goes here! }
function renderHeader(){ ...bimbom... yous stuff goes here! }
<Flatlist
renderItem={this.renderMyItem()}
ListHeaderComponent={this.renderHeader()}
...
/>
这里发生了什么?
如果您的组件已加载并将保存在内存中,那么您的函数 renderMyItem
和 renderHeader
都将执行一次。因此,每次调用其中一个函数时,都会调用对内存中保存它们的位置的引用。
在另一种情况下,Arrow-Functions ()=>{...}
在当前上下文中执行并在内存中生成一个新的引用,每次调用时,因为.. 说清楚:你 以这种方式定义 & 调用一个函数。
我遇到了同样的问题,但接受的答案对我不起作用。在这里出现问题是因为每当文本更改(如 onChangeText
中定义)导致重新渲染时,我们都会更新状态。因此我想出了另一个解决方案;
首先我创建了另一个与状态或道具无关的字典对象newState
。所以在改变 newState
dict 时,不会引起重新渲染。那么;
newState = {}
<TextInput onChangeText={text => this.newState.value = text} />
然后我在 onEndEditing
上更改了状态(根据您的问题和我的问题,这是必要的);
<TextInput onChangeText={text => this.newState.value = text} onEndEditing={this.setSearch} />
这里是setSearch
setSearch= () => {
this.setState({couponCode: this.newState.value})
delete this.newState.value;
}
我正在设置状态后删除密钥,因为下次它不会正确更新。
如果您使用的是函数式组件,则不要对 FlatList 的页眉和页脚组件使用箭头函数 () => (...)
,而是仅 return 您的页眉和页脚组件,如下例所示。
<FlatList
...
ListHeaderComponent={(<View><Text>Header</Text></View>)}
ListFooterComponent={(<View><Text>Footer<Text></View>)}
/>
我在 flatlist 的页脚中放置了一个输入字段,但是当我尝试键入任何内容时,由于重新呈现 flatlist 页脚,它会自动关闭键盘..
我试图从 Scrollview 嵌套平面列表,但这带来了警告..
如何阻止重新呈现页脚?我可以在不嵌套 Scrollview 的平面列表的情况下解决这个问题吗?
<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>
)}
/>
Arrow-Funktions“总是”执行并在内存中创建一个新的引用。这样,如果组件将被执行,它们将始终重新渲染。
出于性能原因,您最好在外部定义您的函数并像这样调用它:
function renderMyItem(){ ...bimbom... yous stuff goes here! }
function renderHeader(){ ...bimbom... yous stuff goes here! }
<Flatlist
renderItem={this.renderMyItem()}
ListHeaderComponent={this.renderHeader()}
...
/>
这里发生了什么?
如果您的组件已加载并将保存在内存中,那么您的函数 renderMyItem
和 renderHeader
都将执行一次。因此,每次调用其中一个函数时,都会调用对内存中保存它们的位置的引用。
在另一种情况下,Arrow-Functions ()=>{...}
在当前上下文中执行并在内存中生成一个新的引用,每次调用时,因为.. 说清楚:你 以这种方式定义 & 调用一个函数。
我遇到了同样的问题,但接受的答案对我不起作用。在这里出现问题是因为每当文本更改(如 onChangeText
中定义)导致重新渲染时,我们都会更新状态。因此我想出了另一个解决方案;
首先我创建了另一个与状态或道具无关的字典对象newState
。所以在改变 newState
dict 时,不会引起重新渲染。那么;
newState = {}
<TextInput onChangeText={text => this.newState.value = text} />
然后我在 onEndEditing
上更改了状态(根据您的问题和我的问题,这是必要的);
<TextInput onChangeText={text => this.newState.value = text} onEndEditing={this.setSearch} />
这里是setSearch
setSearch= () => {
this.setState({couponCode: this.newState.value})
delete this.newState.value;
}
我正在设置状态后删除密钥,因为下次它不会正确更新。
如果您使用的是函数式组件,则不要对 FlatList 的页眉和页脚组件使用箭头函数 () => (...)
,而是仅 return 您的页眉和页脚组件,如下例所示。
<FlatList
...
ListHeaderComponent={(<View><Text>Header</Text></View>)}
ListFooterComponent={(<View><Text>Footer<Text></View>)}
/>