【发布时间】:2019-07-15 18:08:05
【问题描述】:
我遇到了 React Router 的一个问题,我试图在卡片内渲染一个组件,但由于某些原因,数据没有显示:
<Link
to={{
pathname: `/job/${job._id}`,
state: job}}>
<p className="place">{job.workplace_name}</p>
<p className="location-job">{job.location}</p>
</Link>
点击这个链接我想渲染:
const Articles = () => {
const query = (id) => {
fetch(`https://someurl.herokuapp.com/job/${id}`).then(res =>
console.log(res.json()))
}
const pathname = window.location.pathname.split('/');
const job_id = pathname[pathname.length - 1];
const job = query(job_id);
let position_name;
let workplace_name;
if (job) {
position_name = job.position_name;
workplace_name = job.workplace_name;
}
return (
<ArticleComponent
position_name={position_name}
workplace_name={workplace_name}
/>
);
};
console.log 上显示所有内容,但由于某种原因,作业未定义,因此我无法将任何数据传递给ArticleComponent,有什么想法吗?在此先感谢您的帮助,我已经卡了很长时间了。
【问题讨论】:
标签: reactjs react-router react-router-v4 react-router-dom