[英]Routing with match() doesn't work in React app
I have a main page with job listings. 我有一个包含工作清单的主页。
Once a user clicks on a specfic job listing, a new site route eg "homepage.com/jobs/12345" shall open the description of the clicked job. 用户点击特定的职位列表后,新的网站路径(例如“ homepage.com/jobs/12345”)将打开该点击的职位的描述。
All of the information for the jobs are stored in an array. 作业的所有信息都存储在一个数组中。 Each job is stored in an object.
每个作业都存储在一个对象中。
My Question: How can I show the description of the clicked job (description key from clicked job item) on a new route like in example URL above? 我的问题:如何在新的路线上显示点击的作业的描述(点击的作业项目的描述键),例如上面的示例URL?
An item in the array looks like this: 数组中的一项如下所示:
{
"id": "12345",
"position": "Marketing Manager",
"company": "Apple",
"description": "Lorem ipsum",
"date": "2018-11-27T23:11:27Z"
},
Routing in App.js looks like this: App.js中的路由如下所示:
<Router>
<React.Fragment>
<Route
exact
path="/"
render={() => (
<Home jobs={filteredJobs} searchChange={this.onSearchChange} />
)}
/>
<Route
path="/jobs/:id"
render={({ match }) => (
<div>
<Description
description={
this.state.jobs.find(job => job.id === match.params.id)
.description
}
/>
</div>
)}
/>
</React.Fragment>
</Router>
Home.js looks like this: Home.js看起来像这样:
render() {
const { jobs, searchChange } = this.props
return (
<React.Fragment>
<Navigation />
<Filter searchChange={searchChange} />
<Categories searchChange={searchChange} />
{jobs.map((job, index) => (
<JobCard
key={index}
id={job.id}
position={job.position}
company={job.company}
image={job.image}
date={job.date}
/>
))}
</React.Fragment>
)}
JobCard.js looks like this: JobCard.js看起来像这样:
render() {
const { company, date, position, image, id } = this.props
return (
<Router>
<Card data-cy="JobCard" to={`/jobs/${id}`}>
<Image src={image} />
<Position>{position}</Position>
<Company>{company}</Company>
<Date>
<Moment format="D MMM">{date}</Moment>
</Date>
</Card>
</Router>
)
}
Thanks! 谢谢!
We are using react-router and redux for fetching data. 我们正在使用react-router和redux来获取数据。
Our way in routes: 我们的路线:
<Route path="jobs/:id" component={ Job }/>
in Job Component you will need to fetch your data: 在作业组件中,您将需要获取数据:
const mapStateToProps = ({ job }) => ({ job });
const mapDispatchToProps = dispatch => ({
actions: bindActionCreators({
fetchJob
}, dispatch)
}
);
@connect(mapStateToProps, mapDispatchToProps)
export default class Job extends Component {
// your code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.