【发布时间】:2021-04-18 00:19:41
【问题描述】:
我需要帮助,在我的控制台中我收到以下消息:TypeError: Cannot read property '0' of undefined
import React, { useState } from 'react';
import { Link } from 'react-router-dom'
function Course(props) {
const courserName = props.match.params.coursername;
const courses = {
reactjs:[
{title:"reactjs title 1",vid:"edblCnJhvdI"},
{title:"reactjs title 2",vid:"MXZvQuG5mQc"},
{title:"reactjs title 3",vid:"VPIrZr8YVrQ"}
],
nodejs: [
{title:"nodejs title 1", vid: "ngwZT1FR5Fw"},
{title:"nodejs title 2", vid: "goOuCIv39e4"},
{title:"nodejs title 3", vid: "https://youtu.be/r2L8ffwx9TY"}
]
}
const [vid, uid] = useState(courses[courserName][0].vid)
const [title, utit] = useState(courses[courserName][0].title)
const renderVideo = () => {
return (
<div class="embed-responsive embed-responsive-16by9">
<iframe className="embed-responsive-item" src={"https://www.youtube.com/embed/" + vid + "?rel=0"} allowFullScreen></iframe>
</div>
)
}
return (
<div>
<h1> {courserName} </h1>
{renderVideo()}
<div className="menu">
{
courses[courserName].map(item=>{
return <a className="nav-link" href="#">{item.title}</a>
})
}
</div>
</div>
);
}
导出默认课程;
【问题讨论】:
-
你的数组是空的
-
courserName 是正确的值吗?
-
是 courserName 是值
-
我解释说我正在学习一个教程,但它适用于教程中的人,它给了我一个错误。 "const [vid, uid] = useState(courses[courserName][0].vid)"
标签: reactjs