简体   繁体   English

React编译器不会接受我的.map函数作为函数

[英]React compiler won't accept my .map function as a function

I can't get to get the complier acknoledge that I am passing an array to the map function, so I keep getting an error that my .map function is not a function. 我无法获得将数组传递给map函数的编译器确认,因此我不断收到错误消息,表明我的.map函数不是函数。 I just want to list links in an unordered list. 我只想在无序列表中列出链接。

I've tried assigning the data to an generic array with in the function, and now it's greatgrandparent's state, where it's assigned is set up to be an array. 我试过将数据分配给函数中的通用数组,现在它是曾祖父母的状态,将其分配给的状态设置为数组。 I even made a close replica of the code off of the reactjs' site to accomplish what I want out of the logic to use the map function. 我什至在reactjs站点附近制作了代码的紧密副本,以实现我想要的逻辑之外的使用map函数的代码。

const mobileMenu = (props) => {

    return (
        <div className={classes.MobileMenu}>
        <h1 id={classes.MobileMenu__Title}>Water Resources</h1>
        <div>
            <button id={classes.LoginButton}>Login</button>
            <button id={classes.RegisterButton}>Register</button>
        </div>
        <div>
        {menuLinks(props.links)}
        </div>
    </div>
    );
}

calls 电话

const menuLinks = (links) => {
    console.log (links);
    let Links = links;
    const listLinks = Links.map((Links) =>
    (<li key={Links.index.toString()}>
    <a href={Links.link}>{Links.name}</a>
    </li>));
    return (

        <ul>           
            {listLinks()}        
        </ul>
);}

Debugger states that object is a link array, but I keep on getting the error that the list links function is undefined. 调试器指出该对象是一个链接数组,但我不断收到错误消息,即列表链接功能未定义。

mainlinks = [
      {name: "Home", link: "#Home"},
      {name: "Calculator", link: "#Calculator"},
      {name: "Reference", link: "#reference"},
      {name: "About", link: "#About"}
]

that is set in state. 处于状态。 ​​​ ​​

The problem is here, 问题在这里,

{listLinks()}

You are trying to execute function listLinks which is not a function. 您正在尝试执行不是函数的函数listLinks。

Just use {listLinks} without parenthesis like below, 只需使用{listLinks}而不带括号,如下所示,

const menuLinks = (links) => {
        console.log(links);
        const listLinks = links.map(link =>{
           return (
              <li key={link.name}>
                  <a href={link.link}>{link.name}</a>
              </li>
           )
        }); 
        return (
            <ul>           
             {listLinks}        
            </ul>
        );
    }

If you are receiving group of object then here you can find iterate over object https://hackernoon.com/5-techniques-to-iterate-over-javascript-object-entries-and-their-performance-6602dcb708a8 如果您要接收一组对象,则可以在对象上进行迭代https://hackernoon.com/5-techniques-to-iterate-over-javascript-object-entries-and-their-performance-6602dcb708a8

Maybe like this ? 也许是这样吗?


const menuLinks = (links) => {    
    return (
        <ul>           
         {
            links.map(link => (
              <li key={link.name}>
                <a href={link.link}>{link.name}</a>
              </li>
            ))
         }        
        </ul>
    );
}

Edit : I saw some stuff that were weird you're using "Links" for the array name and the map element name i had some issues with this 编辑:我看到了一些奇怪的东西,您正在使用“链接”作为数组名称和地图元素名称,对此我遇到了一些问题

In the return, listLinks is not a function but a new array 作为回报,listLinks不是函数,而是新数组

Or like people said in the comments maybe it's not an array :x 或者像人们在评论中说的那样,它可能不是数组:x

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

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