我有一个模式,用户可以从中 Select 类别,然后将该类别加载到他们个人资料页面上的平面列表中. Select 第一个类别后,它会正确加载所需的格式:

enter image description here

当用户从模式中 Select 并添加第二项时,这些项将消失,如下所示:

enter image description here

And when a third item is added, this error is prompted: enter image description here

CODE:

用户从中 Select 的类别数组,以及钩子状态:

const [catData, setCatData] = React.useState([])
const [catsSelected, setCatsSelected] = React.useState([])

const categoryData=[
    {
        id: 1,
        name: "CatOne",
    },
    {
        id: 2,
        name: "CatTwo",
    },
    {
        id: 3,
        name: "CatThree",
    }]

用户 Select 所需类别后调用的函数:

const onSelectCategory = (itemSelected, index) => {
        let newData = categories.map(item => {
            if (item.id === itemSelected.id) {
                return {
                    ...item,
                    selected: !item.selected
                }
            }
            return {
                ...item,
                selected: item.selected
            }
        })
        selectedData = newData.filter(item => item.selected === true)
        setCatData(selectedData)
    }
// Note, the above code is as such due to initially wanting the user to be able to select multiple categories at a time, however,
// I've decided that one category at a time would suffice, and I just modified the above function to fit that need (I will tidy it up later).

用户确认所选类别后调用的函数:

const catSave = () => {
        if(catData.length > 0){
            if(catsSelected.length < 1){
                setCatsSelected(catData)
            }
            else{
                testData = catsSelected
                testData = testData.push(catData[0])
                setCatsSelected(testData)
            }
        }
        setModalVisible(!modalVisible)
    }

以及所选类别加载到的平面列表:

<FlatList 
   data={catsSelected}
   horizontal
   showsHorizontalScrollIndicator={false}
   keyExtractor={item => `${item.id}`}
   renderItem={renderCats}
                                    
/>

以下是修改时 Select 的CATS的控制台日志(log),以供参考:

[] // when it is initialized
[{"id": 1, "name": "CatOne", "selected": true}] // first item added
[{"id": 1, "name": "CatOne", "selected": true}, {"id": 2, "name": "CatTwo", "selected": true}] // second item added, the FlatList is now invisible
// Error prompts after third item is added.

我需要的是平面列表不可见,并且在添加第三项后不提示此错误,有人确定为什么会发生这种情况吗?

谢谢你的帮助.

推荐答案

Issue

  1. 你正在变异状态
  2. Array.prototype.push的结果保存到状态,即新的数组长度,而不是更新的数组
  3. 在随后的渲染中,当catsSelected不再是一个数组,而是一个数字时,push方法不存在

Array.prototype.push

100方法将一个或多个元素添加到

const catSave = () => {
  if (catData.length > 0) {
    if (catsSelected.length < 1) {
      setCatsSelected(catData);
    } else {
      testData = catsSelected;              // <-- reference to state
      testData = testData.push(catData[0]); // <-- testData.push mutates state
      setCatsSelected(testData);            // <-- testData now new array length
    }
  }
  setModalVisible(!modalVisible);
}

Solution

使用功能状态更新将前一个stat的数组中的更新正确排队.浅层复制前一个状态的数组并附加新数据.

const catSave = () => {
  if (catData.length) {
    setCatsSelected(catsSelected => {
      if (catsSelected.length < 1) {
        return catData;
      }
      return [
        ...catsSelected,
        catData[0],
      ]
    });
  }
  setModalVisible(modalVisible => !modalVisible);
}

Javascript相关问答推荐

如何为GrapesJS模板编辑器创建自定义撤销/重复按钮?

使用AJX发送表单后,$_Post看起来为空

我开始使用/url?q=使用Cheerio

通过嵌套模型对象进行Mongoose搜索

Rehype将hashtag呈现为URL

有没有可能使滑动img动画以更快的速度连续?

自定义高图中的x轴标签序列

空的结果抓取网站与Fetch和Cheerio

如何从网站www.example.com获取表与Cheerio谷歌应用程序脚本

S文本内容和值不必要的不同

如何在文本字段中输入变量?

在数组中查找重叠对象并仅返回那些重叠对象

在css中放置所需 colored颜色 以填充图像的透明区域

搜索功能不是在分页的每一页上进行搜索

try 将Redux工具包与MUI ToggleButtonGroup组件一起使用时出错

使每个<;li>;元素的 colored颜色 与随机生成的 colored颜色 列表不同(不重复

递增/递减按钮React.js/Redux

如何阻止外部脚本进入顶层导航

:host::ng-Deep不将样式应用于material 复选框

从客户端更新MongoDB数据库