我得到了这个错误:无法读取未定义的属性(读取’params’)

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

I am getting this error Cannot read properties of undefined (reading 'params')

问题

以下是您提供的内容的中文翻译:

当我尝试从数据库获取房屋详情时,我遇到了这个错误。

houseinfo.js

import React, { useState, useEffect } from 'react';

const HouseInfo = ({ match }) => {
  const [listings, setListings] = useState(null);
  const id = match.params.id; // 从URL获取房屋ID

  useEffect(() => {
    // 根据房屋ID从API获取房屋数据
    const fetchListings = async () => {
      try {
        const response = await fetch(`http://localhost:80/api/houseinfo/${id}`);
        const data = await response.json();
        setListings(data);
      } catch (error) {
        console.error('获取房屋时出错:', error);
      }
    };
    fetchListings();
  }, [id]);

  if (!listings) {
    return <div>加载中...</div>;
  }

  return (
    <div>
      <h1>房屋信息</h1>
      <ul>
        <li>标题{listings.title}</li>
        <li>位置{listings.location}</li>
        <li>价格{listings.price}</li>
      </ul>
    </div>
  );
};

export default HouseInfo;

这是我的 app.js

<Route path="/houseinfo/:id" element={<HouseInfo />} />

我期望从数据库获取数据并在页面上列出它,但我遇到了这个错误。我对React相对新手,不知道错误是什么,我在跟随YouTube教程,但它现在无法正常工作。

英文:

I got this error when I am trying to get house details from database.

houseinfo.js

import React, { useState, useEffect } from &#39;react&#39;;

const HouseInfo = ({ match }) =&gt; {
  const [listings, setListings] = useState(null);
  const id = match.params.id; // Get house ID from URL

  useEffect(() =&gt; {
    // Fetch house data from API based on house ID
    const fetchListings = async () =&gt; {
      try {
        const response = await fetch(`http://localhost:80/api/houseinfo/${id}`);
        const data = await response.json();
        setListings(data);
      } catch (error) {
        console.error(&#39;Error fetching house:&#39;, error);
      }
    };
    fetchListings();
  }, [id]);

  if (!listings) {
    return &lt;div&gt;Loading...&lt;/div&gt;;
  }

  return (
    &lt;div&gt;
      &lt;h1&gt;House Information&lt;/h1&gt;
      &lt;ul&gt;
        &lt;li&gt;Title: {listings.title}&lt;/li&gt;
        &lt;li&gt;Location: {listings.location}&lt;/li&gt;
        &lt;li&gt;Price: {listings.price}&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  );
};

export default HouseInfo;

This is my app.js

&lt;Route path=&quot;/houseinfo/:id&quot; element={&lt;HouseInfo /&gt;} /&gt;

I am expecting to get the data from the database and list it in the page but I get that error. I am fairly new to react so I don't know what the error is, I was follwoing an youtube tutorial but it is now working

答案1

得分: 0

如果你使用React-router,你可以使用同一包中的useParams()钩子,它会返回一个包含你的URL参数的对象。

在你的代码中会像这样:

import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router';

const HouseInfo = ({ match }) => {
  const [listings, setListings] = useState(null);
  const {id} = useParams(); // 解构返回的对象以只获取"id"值

  useEffect(() => {
    // 你的效果的其余部分
    // ...
  });

export default HouseInfo;
英文:

Assuming you use React-router you can use the hook useParams() from the same package, this will return an object containing your url parameters.

It would look like this in your code:

import React, { useState, useEffect } from &#39;react&#39;;
import { useParams } from &#39;react-router&#39;

const HouseInfo = ({ match }) =&gt; {
  const [listings, setListings] = useState(null);
  const {id} = useParams(); // destructure the returned object to get only the &quot;id&quot; value

  useEffect(() =&gt; {
    //rest of your effect here
    ...
};

export default HouseInfo;

huangapple
  • 本文由 发表于 2023年4月17日 16:42:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033231.html
匿名

发表评论

匿名网友

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

确定