简体   繁体   English

反应 map “类型错误:无法读取未定义的属性‘映射’”

[英]React map "TypeError: Cannot read property 'map' of undefined"

This below is Navbar.js and MenuItems.js Error is https://1drv.ms/u/s?ArTg3PgWw2jtgQ77RY23uF1tkSoA?e=YDsLlK I pull data from array MenuItems.js and display with map in Navbar.js, error is saying "TypeError: Cannot read property 'map' of undefined".下面是 Navbar.js 和 MenuItems.js 错误是https://1drv.ms/u/s?ArTg3PgWw2jtgQ77RY23uF1tkSoA?e=YDsLlK我从数组 MenuItems.js 中提取数据并显示 Z1D78DC8ED51224E5AEZB51 中的错误是“Navbar11 中的错误”类型错误:无法读取未定义的属性“地图”。 Why it occurs?为什么会发生?

  import React, { Component } from 'react'
import { MenuItems } from './MenuItems'

class Navbar extends Component {
render () {
    return (
        <nav className="NavbarItems">
            <h1 className="NavbarLogo">React</h1>
        <div className="menu-icon">

        </div>
        <ul>
        {MenuItems.map((item, index) => {
            return (
         <li key={index}>
             <a className={item.cName} href={item.url}>
                {item.title}


</a>
                 </li>

            )
        })}
        
        </ul>
        </nav>
    )
}

} }

export default Navbar;导出默认导航栏;

menuitems.js菜单项.js

    const MenuItems = [
{
    title:'Home',
    url:'/',
    cName:'nav-links',
},
{
    title:'Services',
    url:'services',
    cName:'nav-links',
},
{
    title:'Products',
    url:'products',
    cName:'nav-links',
},
{
    title:'Contact Us',
    url:'contact-us',
    cName:'nav-links',
},
{
    title:'Sign Up',
    url:'sign-up',
    cName:'nav-links-mobile',
},
]

Yeah, Thats too bit easy.是的,这太容易了。 You just need to export the MenuItems from MenuItems.js file.Just like this one您只需要从 MenuItems.js 文件中导出 MenuItems。就像这个一样

export const MenuItems =[ {}, {}] ; export const MenuItems =[ {}, {}] ;

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

相关问题 类型错误:无法读取 React 中未定义的属性“map” - TypeError: Cannot read property 'map' of undefined in React TypeError:无法读取未定义的属性“地图”(反应) - TypeError: Cannot read property 'map' of undefined (React) React-TypeError:无法读取未定义的属性“ map” - React - TypeError: Cannot read property 'map' of undefined React JS Map TypeError:无法读取未定义的属性“地图” - React JS Map TypeError: Cannot read property 'map' of undefined 类型错误:无法读取未定义的属性“地图”,在反应钩子 crud 应用程序中 - TypeError: Cannot read property 'map' of undefined, in react hooks crud application react js-× TypeError:无法读取未定义的属性“映射” - react js-× TypeError: Cannot read property 'map' of undefined React TypeError:在传递道具时无法读取未定义的属性“map” - React TypeError: Cannot read property 'map' of undefined on passing props TypeError:无法读取未定义的属性“ map” React,Redux - TypeError: Cannot read property 'map' of undefined | React, Redux 使用React的新功能:TypeError:无法读取未定义的属性“ map” - New using React: TypeError: Cannot read property 'map' of undefined TypeError:无法读取未定义的酶和摩卡反应测试的属性“ map” - TypeError: Cannot read property 'map' of undefined react test with enzyme and mocha
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM