【问题标题】:Uncaught TypeError: Cannot read property 'map' of undefined at {Component}.render in ReactJs未捕获的类型错误:无法读取 ReactJs 中 {Component}.render 处未定义的属性“映射”
【发布时间】:2018-09-30 05:07:25
【问题描述】:

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

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

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
};

并且想要子组件中的道具数据..

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>
    );
}
}

我正在学习教程,但收到此错误,我是 reactjs 新手,请帮助....

【问题讨论】:

    标签: javascript arrays reactjs


    【解决方案1】:

    您没有将“项目”道具传递给侧边栏。你只是传递名字。你需要做&lt;Sidebar items={items}&gt;

    【讨论】:

    • 赞成。接得好。正如@Bhojendra 提到的,这也需要在执行 .map 之前进行条件检查
    • 这对我有用,这是一个愚蠢的错误,我将名称传递给 当我将项目传递给侧边栏时它可以工作 顺便说一句好抓住!!谢谢@Ignacio
    【解决方案2】:

    侧边栏组件中的道具,您传递为name。所以,这样做:

    this.props.name && this.props.name.map(...)
    

    map 方法只有在 name props 没有未定义的情况下才会起作用。并且有必要检查它是否有价值。因为在初始渲染时(即使为 null 也会渲染),使用 map 可能会出错。因此,检查它是一个好习惯。

    【讨论】:

      【解决方案3】:

      console.log(this.props) 添加到您的Sidebar 的渲染方法中以查看您收到的内容。

      【讨论】:

      • super(props) 的目的不只是为了让 this.props 在构造函数中可用
      • 你是对的。我的回答是不正确的,尽管在侧边栏渲染中使用 console.log 会有助于暴露项目和名称道具之间的不匹配。
      猜你喜欢
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多