简体   繁体   English

更新 state 时 map 不是 function

[英]map is not a function when updating state

I have a bunch of links that I render on a page.我有一堆我在页面上呈现的链接。 I created a function that changes a specific prop from false to true depending if the link is active or not.我创建了一个 function,它根据链接是否处于活动状态,将特定道具从false更改为true This is triggered on click.这是在点击时触发的。

When I view the result of the new set of links I can see that the active prop has changed but the rendering stops working.当我查看新链接集的结果时,我可以看到活动道具已更改,但渲染停止工作。

code:代码:

The links:链接:

const links: Array<LinkObj> = [
  {
    id: 1,
    text: 'A',
    icon: 'fa-chair',
    active: true,
    path: routePoints.A,
  },
  {
    id: 2,
    text: 'P',
    icon: 'fa-info-circle',
    active: false,
    path: routePoints.p,
  },

  {
    id: 3,
    text: 'H',
    icon: 'fa-h-square',
    active: false,
    path: routePoints.O,
  },
  {
    id: 4,
    text: 'U',
    icon: 'fa-h-square',
    active: false,
    path: routePoints.P,
  },

  {
    id: 5,
    text: 'UP',
    icon: 'fa-h-square',
    active: false,
    path: routePoints.PT,
  },
];

inside of the render:渲染内部:

const [linksToRender, setlinksToRender] = useState(links);

 const navLinkClicked = (event: any, obj: LinkObj, path: string) => {
    event.preventDefault();
    const newLinks = linksToRender.map(link => {
      if (link.id === obj.id) link.active = true;
      else link.active = false;

      return link;
    });

    console.log('NEW L', newLinks);

    setlinksToRender(prevLinks => ({ ...prevLinks, ...newLinks }));
  };

After the last line where I update the links the page turns red and I get:在我更新链接的最后一行之后,页面变为红色,我得到:

TypeError: linksToRender.map is not a function类型错误:linksToRender.map 不是 function

What am I doing wrong?我究竟做错了什么?

  • When you call setlinksToRender(prevLinks => ({...prevLinks, ...newLinks }));当您调用setlinksToRender(prevLinks => ({...prevLinks, ...newLinks })); on navLinkClicked , the state linksToRender becomes an object.navLinkClicked上,state linksToRender变为 object。

  • Change setlinksToRender(prevLinks => ({...prevLinks, ...newLinks }));更改setlinksToRender(prevLinks => ({...prevLinks, ...newLinks })); to setlinksToRender(prevLinks => ([...prevLinks, ...newLinks ]));setlinksToRender(prevLinks => ([...prevLinks, ...newLinks ]));

When you call you should just pass the new array to it:当您调用时,您应该将新数组传递给它:

setlinksToRender(newLinks);

Also please notice that if you do this:另请注意,如果您这样做:

setlinksToRender(prevLinks => ([ ...prevLinks, ...newLinks ]));

You will be adding newLinks to the prevLinks which is probably not what you want.您将向prevLinks添加newLinks ,这可能不是您想要的。

You should understand one thing about spread operator ...您应该了解有关传播运算符的一件事...

Spread operator just create a new copy of the existing memory item (array/object).扩展运算符只需创建现有 memory 项目(数组/对象)的新副本。 So, here you should change所以,在这里你应该改变

setlinksToRender(prevLinks => ({ ...prevLinks, ...newLinks }));

to

setlinksToRender(prevLinks => ([ ...prevLinks, ...newLinks ]));

to create a new array, by combining the two arrays 'prevLinks' and 'newLinks'.通过组合两个 arrays 'prevLinks' 和 'newLinks' 创建一个新数组。

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

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