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

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

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

问题

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

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

houseinfo.js

  1. import React, { useState, useEffect } from 'react';
  2. const HouseInfo = ({ match }) => {
  3. const [listings, setListings] = useState(null);
  4. const id = match.params.id; // 从URL获取房屋ID
  5. useEffect(() => {
  6. // 根据房屋ID从API获取房屋数据
  7. const fetchListings = async () => {
  8. try {
  9. const response = await fetch(`http://localhost:80/api/houseinfo/${id}`);
  10. const data = await response.json();
  11. setListings(data);
  12. } catch (error) {
  13. console.error('获取房屋时出错:', error);
  14. }
  15. };
  16. fetchListings();
  17. }, [id]);
  18. if (!listings) {
  19. return <div>加载中...</div>;
  20. }
  21. return (
  22. <div>
  23. <h1>房屋信息</h1>
  24. <ul>
  25. <li>标题{listings.title}</li>
  26. <li>位置{listings.location}</li>
  27. <li>价格{listings.price}</li>
  28. </ul>
  29. </div>
  30. );
  31. };
  32. 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

  1. import React, { useState, useEffect } from &#39;react&#39;;
  2. const HouseInfo = ({ match }) =&gt; {
  3. const [listings, setListings] = useState(null);
  4. const id = match.params.id; // Get house ID from URL
  5. useEffect(() =&gt; {
  6. // Fetch house data from API based on house ID
  7. const fetchListings = async () =&gt; {
  8. try {
  9. const response = await fetch(`http://localhost:80/api/houseinfo/${id}`);
  10. const data = await response.json();
  11. setListings(data);
  12. } catch (error) {
  13. console.error(&#39;Error fetching house:&#39;, error);
  14. }
  15. };
  16. fetchListings();
  17. }, [id]);
  18. if (!listings) {
  19. return &lt;div&gt;Loading...&lt;/div&gt;;
  20. }
  21. return (
  22. &lt;div&gt;
  23. &lt;h1&gt;House Information&lt;/h1&gt;
  24. &lt;ul&gt;
  25. &lt;li&gt;Title: {listings.title}&lt;/li&gt;
  26. &lt;li&gt;Location: {listings.location}&lt;/li&gt;
  27. &lt;li&gt;Price: {listings.price}&lt;/li&gt;
  28. &lt;/ul&gt;
  29. &lt;/div&gt;
  30. );
  31. };
  32. 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参数的对象。

在你的代码中会像这样:

  1. import React, { useState, useEffect } from 'react';
  2. import { useParams } from 'react-router';
  3. const HouseInfo = ({ match }) => {
  4. const [listings, setListings] = useState(null);
  5. const {id} = useParams(); // 解构返回的对象以只获取"id"值
  6. useEffect(() => {
  7. // 你的效果的其余部分
  8. // ...
  9. });
  10. 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:

  1. import React, { useState, useEffect } from &#39;react&#39;;
  2. import { useParams } from &#39;react-router&#39;
  3. const HouseInfo = ({ match }) =&gt; {
  4. const [listings, setListings] = useState(null);
  5. const {id} = useParams(); // destructure the returned object to get only the &quot;id&quot; value
  6. useEffect(() =&gt; {
  7. //rest of your effect here
  8. ...
  9. };
  10. 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:

确定