简体   繁体   English

如何在反应js中移动列表中的项目

[英]How to move an item in a list in react js

`Hi guys, I need help with react js, what happens is that I want to move an item from a specific list to the last place, but when I move it, it doesn't load the data I had before, unfortunately I'm very new to react js and ps isn't I know all of them, so I turn to this community in search of help and soon I will upload the code and I hope you can help me, thanks. `大家好,我需要 React js 的帮助,发生的事情是我想将一个项目从特定列表移动到最后一个位置,但是当我移动它时,它不会加载我之前拥有的数据,不幸的是我'我对 React js 和 ps 很陌生,不是我都知道,所以我转向这个社区寻求帮助,很快我会上传代码,希望你能帮助我,谢谢。

const [categories, setCategories] = useState<CategoriesWithSubsCategories>()

const listSelectCategory = categories?.list.map(cat =\> {
  const { category, id } = cat.category
  return {
    value: id,
    label: category
  }
}).filter(cat =\> cat.label !== 'Otros')
listSelectCategory?.sort((a, b) =\> a.label.localeCompare(b.label)).push({ value: 'Otros', label: 'Otros' })``

As you have seen above, try using the filter attribute to filter that category and then use the push attribute to add it to the end of the list, it worked but not how it should work because the category that I want to put at the end of the list should bring me some subcategories but it doesn't It does and that is where my problem lies.`正如您在上面所看到的,尝试使用 filter 属性来过滤该类别,然后使用 push 属性将其添加到列表的末尾,它有效但不是它应该如何工作,因为我想放在最后的类别列表中的应该给我带来一些子类别,但它没有,这就是我的问题所在。

try to set the result using the setState variable.尝试使用 setState 变量设置结果。 I don't see that anywhere.我在任何地方都看不到这一点。

If you want to access the data from the variable named categories add this line at the bottom of your code, it will update the value of the variable with the setState function.如果您想从名为categories的变量中访问数据,请在代码底部添加此行,它将使用 setState function 更新变量的值。

setCategories(listSelectCategory)

You can do something like this:你可以这样做:

const [categories, setCategories] = useState<CategoriesWithSubsCategories>();

...

// suppose categories is defined
setCategories((prev) => {
    const newList = [...prev.list];
    newList.push(newList.splice(newList.findIndex((item) => item.label === "Otros"),1)[0]);
    return {
        ...prev,
        list: newList
    };
});

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

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