开发者问题收集

我收到以下消息:“未捕获的 TypeError:无法读取 React 中未定义的属性”

2023-03-06
817

我想要一个显示四个住宿列表的主页,并能够通过点击跳转到每个住宿的特定页面。

我不知道错误来自哪里。

我的代码产生

Uncaught TypeError with a message of "Cannot read properties of undefined (reading 'map')"

APP.js:

import React from 'react';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import AccommodationList from './components/AccommodationList';
import AccommodationDetails from './components/AccommodationDetails';
//import accommodationsData from './data/accommodations.json';
import './styles/accommodations.css';

export default function App() {
    return (
        <BrowserRouter>
            <Routes>
                <Route path="/" element={<AccommodationList/>}/>
                <Route path="/accommodation/:id" element={<AccommodationDetails/>}/>
            </Routes>
        </BrowserRouter>
    );
}

ACCOMMODATIONDETAILS.js:

import React from 'react';
import accommodationsData from '../data/accommodations.json';
import '../styles/accommodations.css';

function AccommodationDetails(props) {
    const accommodation = accommodationsData.find((a) => a.id === props.match.params.id);

    return (
      <div>
        <h2>{accommodation.title}</h2>
        <p>{accommodation.description}</p>
        <p>Prix : {accommodation.price}€</p>
        <img src={accommodation.imageURL} alt={accommodation.altTxt} />
      </div>
    );
  }

export default AccommodationDetails;

ACCOMMODATIONLIST.js

import React from 'react';
import { Link } from 'react-router-dom';
import allaccommodations from '../data/accommodations.json';
import '../styles/accommodations.css';

export default function AccommodationList(props) {
    return (
        <div>
            <div>
                <h1>Chez Nestor</h1>
                <AccommodationList allaccommodations={allaccommodations} />
            </div>
            <div className='container'>
                {props.allaccommodations.map(accommodation => (
                    <div key={accommodation.id}>
                        <figure>
                            <img src={accommodation.imageURL} alt={accommodation.altTxt} />
                            <figcaption>
                                <h2>{accommodation.title}</h2>
                                <p>{accommodation.description}</p>
                                <p>Prix : {accommodation.price}€</p>
                                <Link to={`/accommodation/${accommodation.id}`}>Voir plus</Link>
                            </figcaption>
                        </figure>
                    </div>
                ))}
            </div>
        </div>
    );
}

console.log(allaccommodations)

我尝试更改名称,或将术语“元素”更改为“组件”。这个错误来自哪里,我该如何修复它?

2个回答

问题始于 AccommodationList ,其中没有将任何 props 传递给组件,因此 props.allaccommodations 未定义。

您似乎还以递归方式渲染 <AccommodationList /> 组件,这似乎不正确且没有必要。它很可能会被删除。

假设您想要渲染导入的 allaccommodations ,它不会位于 props 对象上,而应该直接映射。

示例:

<Route path="/" element={<AccommodationList />} /> // <-- no passed props
import React from 'react';
import { Link } from 'react-router-dom';
import allaccommodations from '../data/accommodations.json'; // <-- imported here
import '../styles/accommodations.css';

export default function AccommodationList() { // <-- no props to reference
  return (
    <div>
      <div>
        <h1>Chez Nestor</h1>
      </div>
      <div className='container'>
        {allaccommodations.map(accommodation => ( // <-- referenced here
          <div key={accommodation.id}>
            <figure>
              <img src={accommodation.imageURL} alt={accommodation.altTxt} />
              <figcaption>
                <h2>{accommodation.title}</h2>
                <p>{accommodation.description}</p>
                <p>Prix : {accommodation.price}€</p>
                <Link to={`/accommodation/${accommodation.id}`}>
                  Voir plus
                </Link>
              </figcaption>
            </figure>
          </div>
        ))}
      </div>
    </div>
  );
}
Drew Reese
2023-03-06

确保 props.allaccommodations 已定义一些值。
然后尝试在其上使用 map

示例:

{
  props.allaccomodations &&
    props.allaccommodations.map((accommodation) => (
      <div key={accommodation.id}>
        <figure>
          <img src={accommodation.imageURL} alt={accommodation.altTxt} />
          <figcaption>
            <h2>{accommodation.title}</h2>
            <p>{accommodation.description}</p>
            <p>Prix : {accommodation.price}€</p>
            <Link to={`/accommodation/${accommodation.id}`}>Voir plus</Link>
          </figcaption>
        </figure>
      </div>
    ));
}
krishnaacharyaa
2023-03-06