简体   繁体   English

第一次单击未呈现正确的数据[第二次单击有效]?

[英]First click not rendering correct data [second click works]?

I have a menu here, made with a horizontal flatlist:我在这里有一个菜单,用水平平面列表制作:

在此处输入图像描述

Code for menu:菜单代码:

<View style={{ position: 'absolute', left: 0, top: 0 }}>
    <FlatList
      horizontal
      data={categories}
      extraData={
      selectedCategory // for single item
    }
      style={styles.flatList}
      renderItem={({ item: rowData }) => (
        <TouchableOpacity
          onPress={() => recalculateCategory(rowData)} 
          style={rowData === selectedCategory ? styles.selected : styles.unselected}
        >
          <Text style={{ fontWeight: 'bold', color: '#FFFFFF', padding: 6 }}>
            { rowData }
          </Text>

        </TouchableOpacity>
      )}
      keyExtractor={(item, index) => item.toString()}
    />
  </View>

Code for recalculateCategory:重新计算类别的代码:

const recalculateCategory = (rowData) => {
  setSelectedCategory(rowData);
  getCollection();
};

code for getCollection: getCollection 的代码:

const getCollection = async() => {
    setIsLoading(true);
    const index = 1;
    const getThoughtsOneCategory = Firebase.functions().httpsCallable('getThoughtsOneCategory');
    await getThoughtsOneCategory({
      index,
      category: selectedCategory,
    }).then((result) => {
      setThoughts(result.data);
      setIsLoading(false);
    }).catch((err) => {
      console.log(err);
    });
  };

How it works is: if you want to see options posts, you click options, and it renders the data accordingly.它的工作原理是:如果您想查看选项帖子,请单击选项,然后它会相应地呈现数据。

I am running into an issue.我遇到了一个问题。

  1. We are on "STOCKS"我们在“股票”

添加

  1. Click on "OPTIONS" [Notice the data is the same as "STOCKS"]点击“OPTIONS”[注意数据与“STOCKS”相同]

在此处输入图像描述

  1. Click on "OPTIONS" again, and we get the correct data再次点击“OPTIONS”,我们得到正确的数据

在此处输入图像描述

How can I fix this requirement to double click on a menu category?如何解决此要求以双击菜单类别? Thanks谢谢

Instead of calling getCollection directly, you could use useEffect to call it whenever the category changes.您可以使用useEffect在类别更改时调用它,而不是直接调用getCollection

Something like:就像是:

useEffect(() => {
  getCollection();
},[selectedCategory])

And then your recalculateCategory would just look like:然后你的recalculateCategory看起来像:

const recalculateCategory = (rowData) => {
  setSelectedCategory(rowData);
};

useEffect runs whenever the parameters in the second argument change.每当第二个参数中的参数更改时, useEffect运行。 You may need to adjust them to fit your use case, but selectedCategory would definitely be in there.您可能需要调整它们以适合您的用例,但selectedCategory肯定会在那里。

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

相关问题 React:第二次点击时呈现数据,但不是第一次点击 - React: Data rendering on second click, but not on first .select()仅适用于第一次单击,而不适用于第二次单击。 - .select() works with first click , not on second click. 点击jQuery不会在第一次点击时触发。 - jQuery on click not triggering on first click..works on second click 尝试访问 JSON 数据在第一次单击时会引发错误,但在第二次单击时有效 - Trying to access JSON data throws error on first click, but works on second click innerHTML 在第一次单击按钮时有效,但在第二次单击时无效 - innerHTML works on first button click but doesn't work on second click 单击动作在第二次单击时起作用 - Click Actions works on second click 第一个按钮点击+第二个按钮点击,利用按钮的数据 - First button click + Second button click, utilize button's data 第一次尝试时,链接点击事件未触发按钮点击事件(仅限IE)。 第二次单击即可工作。 - link click event not firing button click event on the first try (IE only). Works on second click. jQuery:button_click事件在第一次单击时不触发,而在第二次单击时起作用,为什么? 如何解决? - jQuery : button_click event not fires on first click and works on second click, why? How to fix it? 导航栏仅在第一次点击时有效,第二次无效 - Navbar works only in first click, in second does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM