FlatList 自动更新或重新渲染 react native

时间:2021-07-14 06:26:14

标签: react-native react-native-flatlist

我是 react native 的新手,我在这个问题上被困了 2 天。 我正在尝试向我的列表中添加一个新项目,该项目作为状态提供给 Flatlist。我读到 FlatList 是一个纯组件,除非通过“extraData”告诉它,否则它不会重新渲染自身,并且当状态值发生更改时,组件会重新渲染自身,但这两个选项不会似乎对我有用。显然,我做错了什么或者我的概念不清楚。 看看我的代码。

import { StatusBar } from 'expo-status-bar';
import React,{useState, useEffect} from 'react';
import { StyleSheet, Text, View, TouchableOpacity, FlatList, SafeAreaView, ToastAndroid } from 'react-native';



export default function App() {

  var dishList = [
    {
      id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba',
      title: 'First Item',
    },
    {
      id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63',
      title: 'Second Item',
    },
    {
      id: '58694a0f-3da1-471f-bd96-145571e29d72',
      title: 'Third Item',
    },
  ]
  const [dishes, setDishes] = useState(dishList)
  var count = 0;

  useEffect(()=>{
    console.log("use")
  },[dishes])

  const CustomDish = (props) => {
    return(
      <View style={styles.item}>
        <TouchableOpacity  >
          <Text style={styles.title}>{props.title} = {props.id}</Text>
        </TouchableOpacity>
      </View>
    )
  };

  const renderItem = ({ item }) => (
    <CustomDish title={item.title} id={item.id} />
  );

  function addItem(){
    count = count + 1;
    console.log("Count: "+ count)
    var i = {
      id: `${count}`,
      title: `${count + " Item"}`,
    }
    dishList.push(i)
    setDishes(dishList)
    console.log("state length: "+ dishes.length)
    console.log("list length: "+ dishList.length)
    
  }

  function renderScreen(){
    return(
      <View style={styles.container}>
      <StatusBar style="auto" />
      <View style={{backgroundColor: 'blue', flex:1,width:'100%', height:"100%"}}>
        <SafeAreaView style={{margin: 20}} >
          <FlatList
            data={dishes}
            renderItem={renderItem}
            keyExtractor={item => item.id}
          />
      </SafeAreaView>
      </View>
      <View style={{flexDirection: 'row'}}>
        <TouchableOpacity style={styles.button} onPress={addItem} >
          <Text style={styles.buttonText} >Add</Text>
        </TouchableOpacity>
        <TouchableOpacity style={styles.button} >
          <Text style={styles.buttonText} >Remove</Text>
        </TouchableOpacity>
      </View>
    </View>
    )
  }

  return (
    <SafeAreaView style={styles.container}>
      {renderScreen()}
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
  button:{alignSelf: 'auto',backgroundColor: 'red', height: 50, width: 100, justifyContent:"center", margin: 10},
  buttonText:{alignSelf: 'center', fontWeight: 'bold'},
  item: {
    backgroundColor: '#f9c2ff',
    padding: 20,
    marginVertical: 8,
    marginHorizontal: 16,
  },
  title: {
    fontSize: 20,
  },

});

我在这里尝试做的是按下“添加”按钮并将一个新项目添加到我的列表中,并显示一个视图来表示它已添加到列表中。所以在 console.log 中,我的列表和状态的值会更新,它的长度会从 3 变为 4,从 4 变为 5 等等,每次我按下按钮但在屏幕上,FlatList 不会呈现任何新组件,请帮忙。

2 个答案:

答案 0 :(得分:0)

你需要这样做

function addItem(){
count = count + 1;
console.log("Count: "+ count)
var i = {
  id: `${count}`,
  title: `${count + " Item"}`,
}
let dishTemp = [...dishes];
dishTemp.push(i)
setDishes(dishTemp)
console.log("state length: "+ dishes.length)
console.log("list length: "+ dishList.length)

}

答案 1 :(得分:0)

count 变量将在每次重新渲染 循环时重置。

您应该将计数变量声明移到应用组件之外

喜欢:

var count = 0;

export default function App() {
    ...
}