【发布时间】:2022-01-01 15:25:18
【问题描述】:
我正在做一个个人React.js 项目。我遇到了useParams 的问题。单击导航栏后,我无法显示地图项。它进入页面,但无法在屏幕上显示。它在屏幕上显示一个空对象。这是我定义useParams 的ItemContainer,我认为代码有错误:
import { useEffect, useState } from "react";
import ItemDetails from "../Itemdetails";
import { useParams } from "react-router-dom";
const ItemContainer = () => {
const [venue, setVenue] = useState({});
const { raceId } = useParams();
useEffect(() => {
fetch("https://www.betright.com.au/api/racing/todaysracing")
.then((res) => {
return res.json();
})
.then(
(data) => {
setVenue(data);
console.log("data useEffect", data);
},
(err) => {
return console.error(err);
}
);
}, []);
console.log("venue container", {venue});
const para = Object.keys(venue).find((findKey) => findKey === raceId);
console.log('para', para)
return (
<ItemDetails key={para} venue={venue} />
);
};
export default ItemContainer;
这是我要在其中显示项目的ItemDetails:
const ItemDetails = ({ venue }) => {
console.log('venue itemDetails', venue)
return (
<>
{venue.slice(0, 5).map(races => (
<>
<p>{races.Venue}</p>
<p>Race Number: {races.Race1?.RaceNumber}</p>
</>
))}
</>
);
}
export default ItemDetails;
这是导航栏:
import { useEffect, useState } from "react";
import { NavLink } from "react-router-dom";
import styles from './Navbar.module.css';
const Navbar = () => {
const [result, setResult] = useState([]);
useEffect(() => {
fetch("https://www.betright.com.au/api/racing/todaysracing")
.then((res) => {
return res.json();
})
.then(
(data) => {
setResult(data);
console.log("data useEffect", data);
},
(err) => {
return console.error(err);
}
);
}, []);
console.log("result of Navbar", result);
return (
<>
<div className={styles.navbarContainer}>
<NavLink to='./races'
className={styles.navbarHome}
style={({ isActive }) => ({
color: isActive ? '#fff' : '#f4f4f4',
background: isActive ? '#7600dc' : '#7600dc',
})}>Home</NavLink>
{Object.entries(result).map(([key, value]) => (
<div className={styles.navbarItems} key={key}>
{value.slice(0, 1).map((i, race) => (
<NavLink
key={i}
to={`/race/${key}`}
style={({ isActive }) => ({
color: isActive ? '#fff' : '#d3d3d3',
background: isActive ? '#7600dc' : '#7600dc',
textDecoration: isActive ? 'none' : 'none'
})}>{key}</NavLink>
))}
</div>
))}
</div>
</>
);
}
export default Navbar;
这是 App.js
const App = () => {
return (
<>
<Router>
<Navbar />
<Routes>
<Route path='/races' element={<Home />} />
<Route path="/" element={<Navigate replace to="/races" />} />
<Route path="/race/:raceId" element={<ItemContainer />} />
</Routes>
</Router>
</>
)
}
这是一个 link 到包含整个代码的代码框。
更新
这是数据的模拟。 API的原始值具有所有不同的值,只有Throughbred,Harness和Greyhound的键是找到的useParams的共同点。这是一个新的link with the whole code plus the data mocked。您还可以在下面找到模型数据:
const data = {
Throughbred: [
{
VenueID: 674,
Venue: "Santa Anita Park",
Race1: {
EventTypeId: 1,
EventId: 6177745,
VenueId: 0,
Venue: null,
CountryCode: null,
RaceNumber: 6,
AdvertisedStartTime: "/Date(1641036900000)/",
ResultStatusId: 0,
SecondsToJump: 609,
HasFixedMarkets: false,
IsOpenForBetting: false,
MarketShortcuts: null,
Results: null,
MasterCategoryName: null,
EventName: null,
},
CountryCode: "USA",
MasterCategoryName: "Overseas-Racing",
},
{
VenueID: 674,
Venue: "Santa Anita Park",
Race1: {
EventTypeId: 1,
EventId: 6177745,
VenueId: 0,
Venue: null,
CountryCode: null,
RaceNumber: 6,
AdvertisedStartTime: "/Date(1641036900000)/",
ResultStatusId: 0,
SecondsToJump: 609,
HasFixedMarkets: false,
IsOpenForBetting: false,
MarketShortcuts: null,
Results: null,
MasterCategoryName: null,
EventName: null,
},
CountryCode: "USA",
MasterCategoryName: "Overseas-Racing",
}
],
Harness: [{
VenueID: 674,
Venue: "Santa Anita Park",
Race1: {
EventTypeId: 1,
EventId: 6177745,
VenueId: 0,
Venue: null,
CountryCode: null,
RaceNumber: 6,
AdvertisedStartTime: "/Date(1641036900000)/",
ResultStatusId: 0,
SecondsToJump: 609,
HasFixedMarkets: false,
IsOpenForBetting: false,
MarketShortcuts: null,
Results: null,
MasterCategoryName: null,
EventName: null,
},
CountryCode: "USA",
MasterCategoryName: "Overseas-Racing",
},
{
VenueID: 674,
Venue: "Santa Anita Park",
Race1: {
EventTypeId: 1,
EventId: 6177745,
VenueId: 0,
Venue: null,
CountryCode: null,
RaceNumber: 6,
AdvertisedStartTime: "/Date(1641036900000)/",
ResultStatusId: 0,
SecondsToJump: 609,
HasFixedMarkets: false,
IsOpenForBetting: false,
MarketShortcuts: null,
Results: null,
MasterCategoryName: null,
EventName: null,
},
CountryCode: "USA",
MasterCategoryName: "Overseas-Racing",
}
],
Greyhound: [
{
VenueID: 674,
Venue: "Santa Anita Park",
Race1: {
EventTypeId: 1,
EventId: 6177745,
VenueId: 0,
Venue: null,
CountryCode: null,
RaceNumber: 6,
AdvertisedStartTime: "/Date(1641036900000)/",
ResultStatusId: 0,
SecondsToJump: 609,
HasFixedMarkets: false,
IsOpenForBetting: false,
MarketShortcuts: null,
Results: null,
MasterCategoryName: null,
EventName: null,
},
CountryCode: "USA",
MasterCategoryName: "Overseas-Racing",
},
{
VenueID: 674,
Venue: "Santa Anita Park",
Race1: {
EventTypeId: 1,
EventId: 6177745,
VenueId: 0,
Venue: null,
CountryCode: null,
RaceNumber: 6,
AdvertisedStartTime: "/Date(1641036900000)/",
ResultStatusId: 0,
SecondsToJump: 609,
HasFixedMarkets: false,
IsOpenForBetting: false,
MarketShortcuts: null,
Results: null,
MasterCategoryName: null,
EventName: null,
},
CountryCode: "USA",
MasterCategoryName: "Overseas-Racing",
}
]
}
export default data;
【问题讨论】:
-
Codesandbox 不适合我,API 返回错误
-
嗨@LuisMontoya 我刚刚检查过,它工作正常。这是同一个链接:codesandbox.io/s/festive-vaughan-070ug
-
您在哪个页面有问题?
/race/:raceId? -
@JoaquinPalacios 我和 Luis 有同样的问题,我在 api 上得到了空结果。我猜,我们还在 1 月 1 日,但你在澳大利亚已经搬到 1 月 2 日了?所以
/racing/todaysracing没有结果。 -
CSB 不起作用,您的 API 端点似乎受到限制,因此它不会为我们提取任何数据。
raceId路由参数正在工作,所以我只能假设它是 API 未返回数据或您的Object.keys(venue).find((findKey) => findKey === raceId)逻辑不正确。您可以在 CSB 中为我们模拟"https://www.betright.com.au/api/racing/todaysracing"数据响应,我们可以从那里进行调试。
标签: javascript reactjs object react-router react-router-dom