未捕获的TypeError:无法在ReactJs中的{Component} .render中读取未定义的属性“map” [英] Uncaught TypeError: Cannot read property 'map' of undefined at {Component}.render in ReactJs

查看:124
本文介绍了未捕获的TypeError:无法在ReactJs中的{Component} .render中读取未定义的属性“map”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的数组传递给侧边栏组件的道具像这样。我想在我的子组件中访问它...

I am Passing my Array to props of sidebar Component like this.and i want to access it in my child component...

当我保存代码时转到浏览器...我收到此错误消息

When i save the code and go to browser... i am getting this error message

import React, { Component } from 'react';
import PropTypes from 'prop-types';

import './styles.scss';

import Sidebar from './../../components/sidebar';
import Header from './../../components/header';

export default class Dashboard extends Component {

buildComponent = props => {
    console.log("Props", props.children);

    var items = [
        {
            "link": "facebook.com",
            "title": "Facebook"
        },
        {
            "link": "google.com",
            "title": "Google"
        }
    ]

    return (
        <div>
            <Header />
            <div className="container-fluid">
                <div className="row">
                    <Sidebar name={items} />
                    <div className="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
                        {props.children}
                    </div>
                </div>
            </div>
        </div>
    );
}

render() {
    return this.buildComponent(this.props);
}
}

Dashboard.propTypes = {
children: PropTypes.any
};

并希望在子组件中显示道具数据..

And want props data here in child component..

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import PropTypes from 'prop-types';




export default class Sidebar extends Component {
constructor() {
    super();

}


render() {
    return (
        <div className="row">
            <div className="col-sm-3 col-md-2 sidebar">
                <ul className="nav nav-sidebar">
                    <li><a href="#">Dashboard</a></li>
                    <li className="fa fa-plus"><Link to="/feed">For Your Approval</Link></li>
                    <li><Link to="/article">Active</Link></li>
                    <li><a href="">{this.props.items.map((title) => <li>{title}</li>)}</a></li>
                    <li><a href="#">Rejected</a></li>
                    <li><a href="#">Send For Approval</a></li>
                    <li><a href="#">Draft</a></li>
                    <li><a href="#">Archied</a></li>

                </ul>
                <ul className="nav nav-sidebar">
                    <li><a href="">Brand Safety</a></li>
                    <li><a href="">Report</a></li>
                </ul>
            </div>
        </div>
    );
}
}

我正在关注教程,但是收到此错误,我我是反应主义的新手,请帮帮....

I am following tutorial, but getting this error, I am new to reactjs , please help....

推荐答案

你没有将项目道具传递给补充工具栏。你只是传递名字。您需要<侧边栏项目= {items}>

you're not passing the 'items' prop to Sidebar. You're just passing name. You need to do <Sidebar items={items}>

这篇关于未捕获的TypeError:无法在ReactJs中的{Component} .render中读取未定义的属性“map”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆