道具中出现“无法读取未定义的属性'map'”

时间:2020-05-30 08:47:27

标签: javascript reactjs react-props

我是新来的反应者。将包含对象的数组作为道具从一个组件传递到另一组件时遇到问题。你们能帮我这个忙吗?

这是我要传递值作为道具的卡信息组件的代码

const CardsInformation = () => {
    const Baba = [
        {
            id: 'b1',
            header: 'Book 1 Name',
            image: 'https://www.google.com/search?q=engineering+books&rlz=1C1CHBF_enIN862IN862&sxsrf=ALeKk01jlMEcOr-2Vd0ev6RaoZZSAqMADg:1589729471894&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjO9dfjm7vpAhV8yjgGHR-zDCoQ_AUoAXoECBMQAw&biw=1536&bih=763#imgrc=OzVmsuZIvusEJM',
            discription: 'This book steps beyond the simple functions of the Clapper, introducing you to a number of products that can be used for everything from controlling lighting levels, watering your lawn, closing your drapes, and managing sundry appliances in your home.',
        }
    ];

    return (
        <div>
            <Card_tabs book={Baba}/>
        </div>
    )
}

这是我正在使用道具的另一个组件,并且在{props.book.map}上显示为“无法读取未定义的属性'map'”

const Card_tabs = (props) => {
   
       
            {props.book.map(user=>{
                return(<h1>{user.header}</h1>)
            })} 
       
    
}

1 个答案:

答案 0 :(得分:-1)

import ReactDOM from "react-dom";
import React from "react";

const App = () => {
  const Baba = [
    {
      id: "b1",
      header: "Book 1 Name",
      image:
        "https://www.google.com/search?q=engineering+books&rlz=1C1CHBF_enIN862IN862&sxsrf=ALeKk01jlMEcOr-2Vd0ev6RaoZZSAqMADg:1589729471894&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjO9dfjm7vpAhV8yjgGHR-zDCoQ_AUoAXoECBMQAw&biw=1536&bih=763#imgrc=OzVmsuZIvusEJM",
      discription:
        "This book steps beyond the simple functions of the Clapper, introducing you to a number of products that can be used for everything from controlling lighting levels, watering your lawn, closing your drapes, and managing sundry appliances in your home."
    }
  ];

  return (
    <div>
      <CardTabs book={Baba} />
    </div>
  );
};

const CardTabs = ({ book }) => {
  return book.map(user => {
    return <h1>{user.header}</h1>;
  });
};

ReactDOM.render(<App />, document.getElementById("root"));