简体   繁体   English

React Native,State 发生变化,但 JSX 条件渲染没有更新 UI

[英]React Native, State Changes, but JSX Conditional Rendering Does not Updated UI

Hello and thank you for your time in advance!您好,提前感谢您的宝贵时间!

I am struggling with a small issue that I have not encountered before, with React not rendering an UI element based on a check function.我正在努力解决一个我以前没有遇到过的小问题,React 没有根据检查 function 呈现 UI 元素。 Basically, what I am trying to make, is a multiple selection filter menu, where when an option is clicked, the dot next to it changes to red.基本上,我想要做的是一个多选过滤器菜单,当单击一个选项时,它旁边的点会变为红色。

For this purpose I append the value of each option to an array, and using array.sort, verify when it is to be added (value is pushed to FilterList) and removed (value is popped from FilterList)为此,我将 append 每个选项的值添加到数组中,并使用 array.sort 验证何时添加(值被推送到 FilterList)和删除(值从 FilterList 中弹出)

The checkfilter function operates normally, and when logging the state, indeed it works as intended with the array being updated.检查过滤器 function 运行正常,并且在记录 state 时,它确实在更新阵列时按预期工作。

However, the JSX code running the checkfilter to render the extra red dot inside the bigger circle, unfortunately does not.然而,运行 checkfilter 以在更大的圆圈内渲染额外的红点的 JSX 代码,不幸的是没有。

Additionally, when the screen is refreshed, the UI is updated normally, with every option clicked, now showing the aforementioned red dot.此外,当屏幕刷新时,UI 会正常更新,每点击一个选项,现在都会显示前面提到的红点。

Why is this happening?为什么会这样? I have tried several hooks, JSX approaches, using imported components and more that I can't even remember, yet the UI will not update oddly.我已经尝试了几个钩子、JSX 方法、使用导入的组件以及更多我什至不记得的东西,但 UI 不会奇怪地更新。

Below you can find a snippet of the code.您可以在下面找到代码片段。 Please bear in mind this is a render function for a flatlist component请记住,这是一个平面列表组件的渲染 function

    const checkFilter = useCallback((element) => {
    return filterList?.some((el: any) => (el == element))
}, [filterList])
const removeFilter = useCallback((cat) => {
    let temparr = filterList
    var index = temparr?.indexOf(cat);
    if (index > -1) {
        temparr?.splice(index, 1);
    }
    setFilterList(temparr)
}, [filterList])
const addFilter = useCallback((cat) => {
    let temparr = filterList;
    temparr.push(cat);
    setFilterList(temparr)
}, [filterList])

const renderFilter = useCallback((item) => {
    return (

        <Pressable
            onPress={() => {
                checkFilter(item?.item) ? removeFilter(item?.item) : addFilter(item?.item);
                console.log(filterList)
            }}
            style={styles.modalOptionWrapper}
        >
            <Text style={styles.modalOptionTitle(checkFilter)}>{item?.item}</Text>
            <View style={styles.modalOptionRowRight}>
                <View style={styles.radioBtn}>
                    {checkFilter(item?.item) ?
                        <View style={styles.radioBtnBullet} />
                        :
                        null
                    }
                </View>
            </View>

        </Pressable>

    )
}, [filterList])

This may not be correct answer but try this.这可能不是正确的答案,但试试这个。 When I say simple basic codes, like this.当我说简单的基本代码时,就像这样。

const ListItems = () => {
  const [filterList, setFilterList] = React.useState([]); // must be array

  const checkFilter = filterList?.some((el) => el === element);
  const removeFilter = useCallback(
    (cat) => {
      // not updating new state, just useing previous state
      setFilterList((prev) => [...prev].filter((el) => el !== cat));
      // used spread iterator
    },
    [] // no need dependency
  );
  const addFilter = useCallback((cat) => {
    // used spread indicator
    setFilterList((prev) => [...prev, cat]);
  }, []);

  const renderFilter = useCallback((item) => {
    // just checking our codes is right with basic elements
    // it may be 'undefined'
    return <Text>{JSON.stringify(item)}</Text>;
  }, []);

  return filterList.map(renderFilter);
};

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

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