【发布时间】:2021-01-11 17:15:20
【问题描述】:
我有一个功能组件,我试图通过钩子访问在 react-router 中命名空间的 URL 参数。
useEffect(() => {
document.title = props.match.params.subject
UserServices.getSubject(props.match.params.subject)
.then(resp => {
if (resp.data.lectures !== undefined) {
setLectures(resp.data.lectures)
mountLecture(resp.data.lectures[0].title)
}
})
}, [])
useEffect(() => {
if(props.match.params.title) mountLecture(props.match.params.title)
else mountLecture(lectures[0].title)
}, [props.match.params])
但是,它一直告诉我 can't read the property title of undefined,即使它出现在控制台日志中。
【问题讨论】:
-
你能控制台记录你的道具和讲座吗?
-
我使用了 2 个 useEffecthooks,让我上传第二个,让你看看它是如何更新的。
标签: reactjs react-hooks