简体   繁体   English

React Native FlatList 仅在 TextInput 或 Picker 更改时更新

[英]React Native FlatList only gets updated when TextInput or Picker changes

I'm trying to dynamically add text from an input field and a picker to a FlatList in React Native.我正在尝试将输入字段和选择器中的文本动态添加到 React Native 中的 FlatList。 The problem I have is that the FlatList doesn't get updated as soon as I hit the button.我遇到的问题是 FlatList 没有在我按下按钮后立即更新。

The item does get added to the list eventually but only after I trigger the onChangeText of the text input or the onValueChange of the picker element.该项目最终确实会添加到列表中,但只有在我触发文本输入的 onChangeText 或选择器元素的 onValueChange 之后。

import React, { Component } from 'react';
import { View, Text, Picker } from 'react-native';
import { TextInput } from 'react-native-paper';
import { TouchableOpacity, FlatList, ScrollView } from 'react-native-gesture-handler';

CreateSetScreen = () => {
    const [value, onChangeText] = React.useState('Placeholder');
    const [pickerValue, setPickerValue] = React.useState("1");
    const [listValues, setListValue] = React.useState([]);
    
    joinData = () => {
        listValues.push({"content": value, "category": pickerValue, "key": listValues.length.toString()});
        setListValue(listValues);
    }

    return(
        <View>
            <TextInput 
                style={{height: 40, borderColor: 'gray', borderWidth: 1}}
                onChangeText={text => onChangeText(text)}
                value={value}
            />
            <Picker
                selectedValue={pickerValue}
                style={{height: 50}}
                onValueChange={(itemValue) => setPickerValue(itemValue)}
            >
                <Picker.Item label="Kategorie 1 - 1%" value="1" />
                <Picker.Item label="Kategorie 2 - 10%" value="2" />
                <Picker.Item label="Kategorie 3 - 20%" value="3" />
                <Picker.Item label="Kategorie 4 - 29%" value="4" />
                <Picker.Item label="Kategorie 5 - 40%" value="5" />
            </Picker>
            <TouchableOpacity
                style={{backgroundColor:'#DDDDDD'}}
                onPress={() => joinData()}
            >
            <Text>Add Item!</Text>
            </TouchableOpacity>
            <FlatList
                data={listValues}
                renderItem={({item}) => (<Text>{item.category}: {item.content}</Text>)}
            />
        </View>
    )
}
    

export default CreateSetScreen;

Any help is appreciated, thank you.任何帮助表示赞赏,谢谢。

It seems .push() works in the setter function from React Hooks but It doesn't.似乎.push()在来自 React Hooks 的设置器 function 中有效,但它没有。 Rather than returning the array itself, .push() returns the length of the array after modification. .push()不是返回数组本身,而是返回修改后的数组长度。

On the other hand, .concat() works to update state, that is .concat() creates a new array and then returns the changed array.另一方面, .concat()用于更新 state,即.concat()创建一个新数组,然后返回更改后的数组。

So just change your joinData function a little bit in this way.因此,只需以这种方式稍微更改您的joinData function 即可。

const joinData = () => {
    setListValue(listValues => listValues.concat({ "content": value, "category": pickerValue, "key": listValues.length.toString() }));
}

Try adding a key extractor to your FlatList :尝试将密钥提取器添加到您的FlatList

<FlatList
  data={listValues}
  renderItem={({item}) => (<Text>{item.category}: {item.content}</Text>)}
  keyExtractor={(item, index) => String(index)}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM