I have this message: "Uncaught TypeError: Cannot read properties of undefined in React"

huangapple go评论141阅读模式
英文:

I have this message: "Uncaught TypeError: Cannot read properties of undefined in React"

问题

I want to have a main page that displays a list of four accommodations, with the ability to jump to a specific page per accommodation with a click.

I don't see where the error comes from.

My code produces

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)

I tried changing names, or the term 'element' by 'component'. Where does this error come from, and how can I fix it?

英文:

I want to have a main page that displays a list of four accommodations, with the ability to jump to a specific page per accommodation with a click.

I don't see where the error comes from.

My code produces

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

APP.js:

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

export default function App() {
    return (
        &lt;BrowserRouter&gt;
            &lt;Routes&gt;
                &lt;Route path=&quot;/&quot; element={&lt;AccommodationList/&gt;}/&gt;
                &lt;Route path=&quot;/accommodation/:id&quot; element={&lt;AccommodationDetails/&gt;}/&gt;
            &lt;/Routes&gt;
        &lt;/BrowserRouter&gt;
    );
}

ACCOMMODATIONDETAILS.js:

import React from &#39;react&#39;;
import accommodationsData from &#39;../data/accommodations.json&#39;;
import &#39;../styles/accommodations.css&#39;;

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

    return (
      &lt;div&gt;
        &lt;h2&gt;{accommodation.title}&lt;/h2&gt;
        &lt;p&gt;{accommodation.description}&lt;/p&gt;
        &lt;p&gt;Prix : {accommodation.price}&lt;/p&gt;
        &lt;img src={accommodation.imageURL} alt={accommodation.altTxt} /&gt;
      &lt;/div&gt;
    );
  }

export default AccommodationDetails;

ACCOMMODATIONLIST.js

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

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

console.log(allaccommodations)

I tried changing names, or the term 'element' by 'component'. Where does this error come from, and how can I fix it?

答案1

得分: 1

问题起始于 AccommodationList,在这里未向组件传递 props,所以 props.allaccommodations 是未定义的。

您还似乎在递归渲染 &lt;AccommodationList /&gt; 组件,这似乎是不正确且不必要的。它可以被删除。

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

示例:

&lt;Route path=&quot;/&quot; element={&lt;AccommodationList /&gt;} /&gt; // &lt;-- 不传递 props
import React from &#39;react&#39;;
import { Link } from &#39;react-router-dom&#39;;
import allaccommodations from &#39;../data/accommodations.json&#39;; // &lt;-- 在这里导入
import &#39;../styles/accommodations.css&#39;;

export default function AccommodationList() { // &lt;-- 没有要引用的 props
  return (
    &lt;div&gt;
      &lt;div&gt;
        &lt;h1&gt;Chez Nestor&lt;/h1&gt;
      &lt;/div&gt;
      &lt;div className=&#39;container&#39;&gt;
        {allaccommodations.map(accommodation =&gt; ( // &lt;-- 在这里引用
          &lt;div key={accommodation.id}&gt;
            &lt;figure&gt;
              &lt;img src={accommodation.imageURL} alt={accommodation.altTxt} /&gt;
              &lt;figcaption&gt;
                &lt;h2&gt;{accommodation.title}&lt;/h2&gt;
                &lt;p&gt;{accommodation.description}&lt;/p&gt;
                &lt;p&gt;Prix : {accommodation.price}&lt;/p&gt;
                &lt;Link to={`/accommodation/${accommodation.id}`}&gt;
                  Voir plus
                &lt;/Link&gt;
              &lt;/figcaption&gt;
            &lt;/figure&gt;
          &lt;/div&gt;
        ))}
      &lt;/div&gt;
    &lt;/div&gt;
  );
}
英文:

The issue starts in AccommodationList where no props are passed to the component, so props.allaccommodations is undefined.

You also appear to be recursively rendering the &lt;AccommodationList /&gt; component, which seems incorrect and unnecessary. It can likely be removed.

Assuming you want to render the imported allaccommodations it won't be located on the props object, it should be mapped directly.

Example:

&lt;Route path=&quot;/&quot; element={&lt;AccommodationList /&gt;} /&gt; // &lt;-- no passed props
import React from &#39;react&#39;;
import { Link } from &#39;react-router-dom&#39;;
import allaccommodations from &#39;../data/accommodations.json&#39;; // &lt;-- imported here
import &#39;../styles/accommodations.css&#39;;

export default function AccommodationList() { // &lt;-- no props to reference
  return (
    &lt;div&gt;
      &lt;div&gt;
        &lt;h1&gt;Chez Nestor&lt;/h1&gt;
      &lt;/div&gt;
      &lt;div className=&#39;container&#39;&gt;
        {allaccommodations.map(accommodation =&gt; ( // &lt;-- referenced here
          &lt;div key={accommodation.id}&gt;
            &lt;figure&gt;
              &lt;img src={accommodation.imageURL} alt={accommodation.altTxt} /&gt;
              &lt;figcaption&gt;
                &lt;h2&gt;{accommodation.title}&lt;/h2&gt;
                &lt;p&gt;{accommodation.description}&lt;/p&gt;
                &lt;p&gt;Prix : {accommodation.price}&lt;/p&gt;
                &lt;Link to={`/accommodation/${accommodation.id}`}&gt;
                  Voir plus
                &lt;/Link&gt;
              &lt;/figcaption&gt;
            &lt;/figure&gt;
          &lt;/div&gt;
        ))}
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

答案2

得分: 0

确保 props.allaccommodations 已经定义并赋有某个值。然后尝试在其上使用 map

示例:

{
  props.allaccommododations &&
    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>
    ));
}
英文:

Make sure props.allaccommodations is defined with some value.<br>
Then try using map on it.

Example:

{
  props.allaccomodations &amp;&amp;
    props.allaccommodations.map((accommodation) =&gt; (
      &lt;div key={accommodation.id}&gt;
        &lt;figure&gt;
          &lt;img src={accommodation.imageURL} alt={accommodation.altTxt} /&gt;
          &lt;figcaption&gt;
            &lt;h2&gt;{accommodation.title}&lt;/h2&gt;
            &lt;p&gt;{accommodation.description}&lt;/p&gt;
            &lt;p&gt;Prix : {accommodation.price}&lt;/p&gt;
            &lt;Link to={`/accommodation/${accommodation.id}`}&gt;Voir plus&lt;/Link&gt;
          &lt;/figcaption&gt;
        &lt;/figure&gt;
      &lt;/div&gt;
    ));
}

huangapple
  • 本文由 发表于 2023年3月7日 06:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定