【问题标题】:FlatList auto update or re-render react nativeFlatList 自动更新或重新渲染本机反应
【发布时间】:2021-07-14 06:26:14
【问题描述】:

我是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 不会呈现任何新组件,请帮忙。

【问题讨论】:

    标签: react-native react-native-flatlist


    【解决方案1】:

    你需要这样做

    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);
    }
    

    【讨论】:

    • 天哪,兄弟,哇。非常感谢你...!有效。我没有足够的声誉,但将此评论标记为我的支持。你能告诉我我的方法和你的方法有什么区别吗?[...]三个点是什么意思?
    • 您将新项目推送到静态数组中,每次尝试将新内容推送到其中时不会重新呈现菜肴状态,在这种方法中,现在您将状态传播到局部变量 disTemp 所以状态中的所有内容都进入dishTemp,然后您推送一个新项目并更新状态,因此它现在将重新呈现,如果您让dishTemp =菜,而不是让dishTemp = [...菜]
    【解决方案2】:

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

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

    喜欢:

    var count = 0;
    
    export default function App() {
        ...
    }
    

    【讨论】:

    • 是的,我注意到并通过将计数设为状态来修复它。感谢您的反馈。
    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 2022-06-15
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多