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 e.g. "homepage.com/jobs/12345" shall open the description of the clicked job.
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?
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:
<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:
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:
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!
javascript reactjs routing match
add a comment |
I have a main page with job listings.
Once a user clicks on a specfic job listing, a new site route e.g. "homepage.com/jobs/12345" shall open the description of the clicked job.
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?
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:
<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:
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:
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!
javascript reactjs routing match
whats the error or problem with your current code? It seems like you correctly passed the description to the new route component now all you need to do is render it
– Shawn Andrews
Nov 28 '18 at 16:30
Well once I click the new route is displayed in the adress field. But the new route does not open. Weirdly, if I reload, then it opens and displays the description correctly
– user9845001
Nov 28 '18 at 16:40
I see. It might help to wrap yourDescription
component inwithRouter(...)
so that it gets notified of paths changes to trigger a rerender
– Shawn Andrews
Nov 28 '18 at 16:42
Can you explain how that would work? I have read into it but don't know where to put it
– user9845001
Nov 28 '18 at 20:58
Import withRouter from react-router, then in your Description component export it likeexport default withRouter(Description)
– Shawn Andrews
Nov 28 '18 at 21:20
add a comment |
I have a main page with job listings.
Once a user clicks on a specfic job listing, a new site route e.g. "homepage.com/jobs/12345" shall open the description of the clicked job.
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?
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:
<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:
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:
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!
javascript reactjs routing match
I have a main page with job listings.
Once a user clicks on a specfic job listing, a new site route e.g. "homepage.com/jobs/12345" shall open the description of the clicked job.
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?
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:
<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:
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:
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!
javascript reactjs routing match
javascript reactjs routing match
asked Nov 28 '18 at 16:22
user9845001
whats the error or problem with your current code? It seems like you correctly passed the description to the new route component now all you need to do is render it
– Shawn Andrews
Nov 28 '18 at 16:30
Well once I click the new route is displayed in the adress field. But the new route does not open. Weirdly, if I reload, then it opens and displays the description correctly
– user9845001
Nov 28 '18 at 16:40
I see. It might help to wrap yourDescription
component inwithRouter(...)
so that it gets notified of paths changes to trigger a rerender
– Shawn Andrews
Nov 28 '18 at 16:42
Can you explain how that would work? I have read into it but don't know where to put it
– user9845001
Nov 28 '18 at 20:58
Import withRouter from react-router, then in your Description component export it likeexport default withRouter(Description)
– Shawn Andrews
Nov 28 '18 at 21:20
add a comment |
whats the error or problem with your current code? It seems like you correctly passed the description to the new route component now all you need to do is render it
– Shawn Andrews
Nov 28 '18 at 16:30
Well once I click the new route is displayed in the adress field. But the new route does not open. Weirdly, if I reload, then it opens and displays the description correctly
– user9845001
Nov 28 '18 at 16:40
I see. It might help to wrap yourDescription
component inwithRouter(...)
so that it gets notified of paths changes to trigger a rerender
– Shawn Andrews
Nov 28 '18 at 16:42
Can you explain how that would work? I have read into it but don't know where to put it
– user9845001
Nov 28 '18 at 20:58
Import withRouter from react-router, then in your Description component export it likeexport default withRouter(Description)
– Shawn Andrews
Nov 28 '18 at 21:20
whats the error or problem with your current code? It seems like you correctly passed the description to the new route component now all you need to do is render it
– Shawn Andrews
Nov 28 '18 at 16:30
whats the error or problem with your current code? It seems like you correctly passed the description to the new route component now all you need to do is render it
– Shawn Andrews
Nov 28 '18 at 16:30
Well once I click the new route is displayed in the adress field. But the new route does not open. Weirdly, if I reload, then it opens and displays the description correctly
– user9845001
Nov 28 '18 at 16:40
Well once I click the new route is displayed in the adress field. But the new route does not open. Weirdly, if I reload, then it opens and displays the description correctly
– user9845001
Nov 28 '18 at 16:40
I see. It might help to wrap your
Description
component in withRouter(...)
so that it gets notified of paths changes to trigger a rerender– Shawn Andrews
Nov 28 '18 at 16:42
I see. It might help to wrap your
Description
component in withRouter(...)
so that it gets notified of paths changes to trigger a rerender– Shawn Andrews
Nov 28 '18 at 16:42
Can you explain how that would work? I have read into it but don't know where to put it
– user9845001
Nov 28 '18 at 20:58
Can you explain how that would work? I have read into it but don't know where to put it
– user9845001
Nov 28 '18 at 20:58
Import withRouter from react-router, then in your Description component export it like
export default withRouter(Description)
– Shawn Andrews
Nov 28 '18 at 21:20
Import withRouter from react-router, then in your Description component export it like
export default withRouter(Description)
– Shawn Andrews
Nov 28 '18 at 21:20
add a comment |
1 Answer
1
active
oldest
votes
We are using react-router and redux for fetching data.
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
}
I am not using Redux, so I think in my case it will be different?
– user9845001
Nov 28 '18 at 16:43
you could try to get in Job Component window.location.pathname and extract id from it and filter for this job?
– Michael Ploeckinger
Nov 28 '18 at 16:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53523893%2frouting-with-match-doesnt-work-in-react-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
We are using react-router and redux for fetching data.
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
}
I am not using Redux, so I think in my case it will be different?
– user9845001
Nov 28 '18 at 16:43
you could try to get in Job Component window.location.pathname and extract id from it and filter for this job?
– Michael Ploeckinger
Nov 28 '18 at 16:48
add a comment |
We are using react-router and redux for fetching data.
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
}
I am not using Redux, so I think in my case it will be different?
– user9845001
Nov 28 '18 at 16:43
you could try to get in Job Component window.location.pathname and extract id from it and filter for this job?
– Michael Ploeckinger
Nov 28 '18 at 16:48
add a comment |
We are using react-router and redux for fetching data.
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
}
We are using react-router and redux for fetching data.
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
}
answered Nov 28 '18 at 16:38
Michael PloeckingerMichael Ploeckinger
1,288317
1,288317
I am not using Redux, so I think in my case it will be different?
– user9845001
Nov 28 '18 at 16:43
you could try to get in Job Component window.location.pathname and extract id from it and filter for this job?
– Michael Ploeckinger
Nov 28 '18 at 16:48
add a comment |
I am not using Redux, so I think in my case it will be different?
– user9845001
Nov 28 '18 at 16:43
you could try to get in Job Component window.location.pathname and extract id from it and filter for this job?
– Michael Ploeckinger
Nov 28 '18 at 16:48
I am not using Redux, so I think in my case it will be different?
– user9845001
Nov 28 '18 at 16:43
I am not using Redux, so I think in my case it will be different?
– user9845001
Nov 28 '18 at 16:43
you could try to get in Job Component window.location.pathname and extract id from it and filter for this job?
– Michael Ploeckinger
Nov 28 '18 at 16:48
you could try to get in Job Component window.location.pathname and extract id from it and filter for this job?
– Michael Ploeckinger
Nov 28 '18 at 16:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53523893%2frouting-with-match-doesnt-work-in-react-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
whats the error or problem with your current code? It seems like you correctly passed the description to the new route component now all you need to do is render it
– Shawn Andrews
Nov 28 '18 at 16:30
Well once I click the new route is displayed in the adress field. But the new route does not open. Weirdly, if I reload, then it opens and displays the description correctly
– user9845001
Nov 28 '18 at 16:40
I see. It might help to wrap your
Description
component inwithRouter(...)
so that it gets notified of paths changes to trigger a rerender– Shawn Andrews
Nov 28 '18 at 16:42
Can you explain how that would work? I have read into it but don't know where to put it
– user9845001
Nov 28 '18 at 20:58
Import withRouter from react-router, then in your Description component export it like
export default withRouter(Description)
– Shawn Andrews
Nov 28 '18 at 21:20