简体   繁体   English

useState 不更新数组

[英]useState dont update the array

iam trying to get some date from firebase using react naitve but iam having some problems , iam using useState to pass the array of objects in my firebase to an array in my code but the useState dont pass the array from firebase and i dont know why this is the handleFunction of getingProducts我正在尝试使用 react naitve 从 firebase 获取一些日期,但我遇到了一些问题,我正在使用 useState 将我的 firebase 中的对象数组传递给我的代码中的一个数组,但是 useState 不从 firebase 传递数组,我不知道为什么会这样是获取产品的handleFunction

const [products, setProducts] = useState([]);

  const getProductHandle = async () => {
    const arr = await getProducts();
    setProducts(arr);
    console.log(arr);
    console.log(products);
  };

  useEffect(async() => {
    await getProductHandle();
  }, []);

and this is the FlatList iam using to show my products这是我用来展示我的产品的 FlatList

<FlatList
        data={products}
        // numColumns={2}
        renderItem={(itemData) => {
          <productCard
            productName={itemData.item.productName}
            price={itemData.item.price}
            details={itemData.item.details}
            type={itemData.item.type}
            image={itemData.item.image}
          />;
        }}
      />

and this is the productCard这是productCard

const productCard = ({ productName, price, image, details, type }) => {
  return (
    <View style={{height:300, width:300 , borderWidth:1}}>
      <Image style={{ width: 100, height: 100 }} source={image} />
      <Text>
        {productName} {price} {details} {type}
      </Text>
    </View>
  );
};

export default productCard;

after running the code it doesnt give me any errors and i tried to console.log the array from my firebase it return with the products in the firebase , but the products array in my useState have zero elements and i dont know how to solve this problem运行代码后,它没有给我任何错误,我尝试从我的 firebase 控制台记录数组,它与 firebase 中的产品一起返回,但我的 useState 中的 products 数组元素为零,我不知道如何解决这个问题

This is the output of the 2 console.log这是 2 console.log 的输出

Your renderItem function does not return anything.您的renderItem函数不返回任何内容。 Thus, the productCard won't be rendered.因此, productCard不会被渲染。

Fix it as follows.修复它如下。

<FlatList
        data={products}
        // numColumns={2}
        keyExtractor={(item, index) => index.toString()}
        renderItem={({item}) => {
          return <productCard
            productName={item.productName}
            price={item.price}
            details={item.details}
            type={item.type}
            image={item.image}
          />;
        }}
      />

You can not pass anonymous async callback to useEffect and you don't even need it, just call the function like this.您不能将匿名异步回调传递给 useEffect 甚至不需要它,只需像这样调用函数即可。

 useEffect(() => { getProductHandle(); }, []);

Plus you should check state value outside the async function because new value will be available after component re-render and not in the async function instantly.另外,您应该检查异步函数之外的状态值,因为新值将在组件重新渲染后可用,而不是立即在异步函数中。

Maybe these codes can solve it.也许这些代码可以解决它。

<FlatList
        data={products}
        // numColumns={2}
        renderItem={(itemData) => ( <productCard
            productName={itemData.item.productName}
            price={itemData.item.price}
            details={itemData.item.details}
            type={itemData.item.type}
            image={itemData.item.image}
          />;
        )}
      />

The difference is that before <productCard you have to write with parentheses or if you wanna with curly brackets, you must be write return不同之处在于,在<productCard之前你必须用括号写,或者如果你想用花括号,你必须写 return

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

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