【问题标题】:TypeError: Cannot read property 'map' of undefined React JsTypeError:无法读取未定义 React Js 的属性“地图”
【发布时间】:2017-11-25 23:40:01
【问题描述】:

我正在尝试通过使用数组将一些数据推送到孙子类, 我所拥有的是一组 id、摘要、详细信息,我使用 id 作为键,摘要就像一个标题,并且在您单击摘要之前隐藏详细信息。

FAQ.js 页面

import React from "react";
import Accordions from '../components/Parts/Accordions';

let AccData = [
   {
     id: 1,
     summary: 'What Loans Do You Provide?',
     details: 'We lend £200 to £3,000 and have repayment terms from 6 
     months, 12 months, 15 months, 18 months or 24 months. You are welcome 
     to apply for a loan of any amount, however your approval will be 
     dependent on credit and affordability checks.',},
  {
     id: 2,
     summary: 'What Loan Terms Do You Offer?',
     details: 'You can borrow between £200 and £400 over a 6 month term. You 
     can borrow between £401 and £850 over a 12 month term. You can borrow 
     between £851 and £1,500 over a 15 month term. You can borrow between 
     £1,501 and £2,000 over a 18 month term, and you can borrow between 
     £2,001 and £3,000 over a 24 month term.'},
  {
     id: 3,
     summary: 'Can I Apply For A Loan?',
     details: 'To be eligible to apply for a loan, you must be at least 18 
     years old, a UK resident and have a UK bank account and debit card. You 
     must also have a net income of at least £700 per month, and be able to 
     comfortably afford the loan repayments.'
  }];

  export default class FAQ extends React.Component {
    render() {

      const hrStyle = {
         lineHeight:"10px",
         margin:"0",
         backgroundColor:"#3ba6f2",
         color:"#3ba6f2",
         border: "solid 2px #3ba6f2",
         width:"100%",
      }

      return (
    <div>
        <div className="container">
            <h1>Frequently Asked Questions</h1>
            <p>&nbsp;</p>
            <h4>The Basics</h4>
            <Accordions AccData={this.props.AccData}/>

        </div>
    </div>
    );
  }
 }

Accordions.js

import React from 'react';
import Accordion from './Accordion';

export default class Accordions extends React.Component {
    render(){
        return(
            <ul>
                {this.props.AccData.map((summ)=> {
                    return <Accordion summ={summ} key={summ.id} />
                })}
            </ul>
        );
    }
}

手风琴.js

import React from 'react';

const styles = {
    active: {
        display: 'inherit',
    },
    inactive: {
        display: 'none',
    }
};

export default class Accordion extends React.Component {
    constructor(){
        super();
        this.state = {
            active: false
        };
        this.toggle = this.toggle.bind(this);
    }

    toggle(){
        this.setState({
            active: !this.state.active
        });
    }

    render(){

        const stateStyle = this.state.active ? styles.active : styles.inactive;

        const hand = {
            cursor:"pointer",
        }

        return (
            <li>
                <p onClick={this.toggle} style={hand}>
                    {this.state.active ? "▼" : "►"} {this.props.summ.summary}
                </p>
                <p style={stateStyle}>
                    {this.props.summ.details}
                </p>
            </li>
        )
    }
}

抱歉,代码混乱,我真的不知道为什么 map 未定义,除了重命名之外,我遵循了一个教程

【问题讨论】:

  • 立即在渲染内部:console.log(this.props.AccData); 映射未定义,因为 AccData 从未分配过。
  • @mkaatman 我如何分配它然后我很新来反应

标签: javascript html css reactjs jsx


【解决方案1】:

原因是AccData需要像这样发送到子组件。而AccData={this.props.AccData} 应该是AccData={AccData}

 return (
    <div>
        <div className="container">
            <h1>Frequently Asked Questions</h1>
            <p>&nbsp;</p>
            <h4>The Basics</h4>
            <Accordions AccData={AccData}/>

        </div>
    </div>
    );

【讨论】:

  • @andywilson 如果解决了这个问题 - 将其标记为答案 ;)
【解决方案2】:

在您的 FAQ.js 中,AccData 只是一个局部变量,它不是 this.props 的成员。

改变

<Accordions AccData={this.props.AccData}/>

...进入

<Accordions AccData={AccData}/>

【讨论】:

    【解决方案3】:

    将您的代码更改为:

    return (
    <div>
        <div className="container">
            <h1>Frequently Asked Questions</h1>
            <p>&nbsp;</p>
            <h4>The Basics</h4>
            <Accordions AccData={AccData}/>
    
        </div>
    </div>
    );
    

    您在FAQs.js 页面中定义AccData,因此只需正常传递它们即可。它们将在范围内,因为您在课堂之外定义了它。因为this.props.AccData 不存在于您的FAQs.js 文件中,所以您变得未定义。但是,它将存在于Accordions.js 中,因为您将它作为道具传递下去。

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 2017-03-26
      • 2021-09-09
      • 2020-09-16
      相关资源
      最近更新 更多