React router v4 - Rendering two components on same route
up vote
1
down vote
favorite
I have these routes
<Route exact path={`/admin/caters/:id`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
When I navigate to the first route I get a cater with a given ID. And the Cater component is rendered
When I navigate to the second route, the CreateCater component is rendered on the page, but I noticed that some redux actions that are used in the Cater component are being run. So both component are somehow being rendered - but I can't figure out why.
Here are the components:
Cater:
class Cater extends Component {
async componentDidMount() {
console.log('Cater component did mount')
const { match: { params: { id }}} = this.props
this.props.get(id)
}
render() {
const { cater } = this.props
if(!cater) {
return null
}
else {
return (
<div>
... component data ...
</div>
)
}
}
}
const mapStateToProps = (state, props) => {
const { match: { params: { id }}} = props
return {
cater: caterSelectors.get(state, id)
}
}
const mapDispatchToProps = (dispatch, props) => {
return {
get: (id) => dispatch(caterActions.get(id))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Cater)
CreateCater:
export default class CreateCaterPage extends Component {
render() {
return (
<React.Fragment>
<Breadcrumbs />
<CaterForm />
</React.Fragment>
)
}
}
When I go to /admin/caters/create' I can see the console.log in the componenDidMount() lifecycle method inside the Cater component.
I cant figure out what I am doing wrong :(
javascript reactjs react-redux react-router react-router-v4
add a comment |
up vote
1
down vote
favorite
I have these routes
<Route exact path={`/admin/caters/:id`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
When I navigate to the first route I get a cater with a given ID. And the Cater component is rendered
When I navigate to the second route, the CreateCater component is rendered on the page, but I noticed that some redux actions that are used in the Cater component are being run. So both component are somehow being rendered - but I can't figure out why.
Here are the components:
Cater:
class Cater extends Component {
async componentDidMount() {
console.log('Cater component did mount')
const { match: { params: { id }}} = this.props
this.props.get(id)
}
render() {
const { cater } = this.props
if(!cater) {
return null
}
else {
return (
<div>
... component data ...
</div>
)
}
}
}
const mapStateToProps = (state, props) => {
const { match: { params: { id }}} = props
return {
cater: caterSelectors.get(state, id)
}
}
const mapDispatchToProps = (dispatch, props) => {
return {
get: (id) => dispatch(caterActions.get(id))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Cater)
CreateCater:
export default class CreateCaterPage extends Component {
render() {
return (
<React.Fragment>
<Breadcrumbs />
<CaterForm />
</React.Fragment>
)
}
}
When I go to /admin/caters/create' I can see the console.log in the componenDidMount() lifecycle method inside the Cater component.
I cant figure out what I am doing wrong :(
javascript reactjs react-redux react-router react-router-v4
Try swapping the<Route />
tags (Put /create first), and let me know if that changes anything
– FrankerZ
Nov 8 at 18:04
Thank you for the reply @FrankerZ. I tried it and it didn't make any difference. I tried making url's different in length, so that create was looking like/admin/cater/create/cater
. And then I didn't have any issues, but that is not how I want to structure my URL
– Roi
Nov 8 at 18:12
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have these routes
<Route exact path={`/admin/caters/:id`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
When I navigate to the first route I get a cater with a given ID. And the Cater component is rendered
When I navigate to the second route, the CreateCater component is rendered on the page, but I noticed that some redux actions that are used in the Cater component are being run. So both component are somehow being rendered - but I can't figure out why.
Here are the components:
Cater:
class Cater extends Component {
async componentDidMount() {
console.log('Cater component did mount')
const { match: { params: { id }}} = this.props
this.props.get(id)
}
render() {
const { cater } = this.props
if(!cater) {
return null
}
else {
return (
<div>
... component data ...
</div>
)
}
}
}
const mapStateToProps = (state, props) => {
const { match: { params: { id }}} = props
return {
cater: caterSelectors.get(state, id)
}
}
const mapDispatchToProps = (dispatch, props) => {
return {
get: (id) => dispatch(caterActions.get(id))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Cater)
CreateCater:
export default class CreateCaterPage extends Component {
render() {
return (
<React.Fragment>
<Breadcrumbs />
<CaterForm />
</React.Fragment>
)
}
}
When I go to /admin/caters/create' I can see the console.log in the componenDidMount() lifecycle method inside the Cater component.
I cant figure out what I am doing wrong :(
javascript reactjs react-redux react-router react-router-v4
I have these routes
<Route exact path={`/admin/caters/:id`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
When I navigate to the first route I get a cater with a given ID. And the Cater component is rendered
When I navigate to the second route, the CreateCater component is rendered on the page, but I noticed that some redux actions that are used in the Cater component are being run. So both component are somehow being rendered - but I can't figure out why.
Here are the components:
Cater:
class Cater extends Component {
async componentDidMount() {
console.log('Cater component did mount')
const { match: { params: { id }}} = this.props
this.props.get(id)
}
render() {
const { cater } = this.props
if(!cater) {
return null
}
else {
return (
<div>
... component data ...
</div>
)
}
}
}
const mapStateToProps = (state, props) => {
const { match: { params: { id }}} = props
return {
cater: caterSelectors.get(state, id)
}
}
const mapDispatchToProps = (dispatch, props) => {
return {
get: (id) => dispatch(caterActions.get(id))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Cater)
CreateCater:
export default class CreateCaterPage extends Component {
render() {
return (
<React.Fragment>
<Breadcrumbs />
<CaterForm />
</React.Fragment>
)
}
}
When I go to /admin/caters/create' I can see the console.log in the componenDidMount() lifecycle method inside the Cater component.
I cant figure out what I am doing wrong :(
javascript reactjs react-redux react-router react-router-v4
javascript reactjs react-redux react-router react-router-v4
asked Nov 8 at 18:01
Roi
579
579
Try swapping the<Route />
tags (Put /create first), and let me know if that changes anything
– FrankerZ
Nov 8 at 18:04
Thank you for the reply @FrankerZ. I tried it and it didn't make any difference. I tried making url's different in length, so that create was looking like/admin/cater/create/cater
. And then I didn't have any issues, but that is not how I want to structure my URL
– Roi
Nov 8 at 18:12
add a comment |
Try swapping the<Route />
tags (Put /create first), and let me know if that changes anything
– FrankerZ
Nov 8 at 18:04
Thank you for the reply @FrankerZ. I tried it and it didn't make any difference. I tried making url's different in length, so that create was looking like/admin/cater/create/cater
. And then I didn't have any issues, but that is not how I want to structure my URL
– Roi
Nov 8 at 18:12
Try swapping the
<Route />
tags (Put /create first), and let me know if that changes anything– FrankerZ
Nov 8 at 18:04
Try swapping the
<Route />
tags (Put /create first), and let me know if that changes anything– FrankerZ
Nov 8 at 18:04
Thank you for the reply @FrankerZ. I tried it and it didn't make any difference. I tried making url's different in length, so that create was looking like
/admin/cater/create/cater
. And then I didn't have any issues, but that is not how I want to structure my URL– Roi
Nov 8 at 18:12
Thank you for the reply @FrankerZ. I tried it and it didn't make any difference. I tried making url's different in length, so that create was looking like
/admin/cater/create/cater
. And then I didn't have any issues, but that is not how I want to structure my URL– Roi
Nov 8 at 18:12
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
/create
matches /:id
, so it makes sense that this route matches. I recommend forcing :id
to look for numeric only:
<Route exact path={`/admin/caters/:id(\d+)`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
Likewise, you can follow @jabsatz's recommendation, use a switch, and have it match the first route that matches. In this case, you would need to ensure that the /admin/caters/create
route is the first <Route />
element matched.
I had no idea you could do(\d+)
. That's super cool.
– David
Nov 8 at 18:21
1
@David react-router under the hood uses path-to-regexp. They explain it a bit in the docs.
– FrankerZ
Nov 8 at 18:23
@FrankerZ thank you! This fixed it for me. Didn't know about the (\d+) either.
– Roi
Nov 8 at 18:25
add a comment |
up vote
1
down vote
The problem is that :id
is matching with create
(so, it thinks "see cater with id create
"). The way to solve this is to put the wildcard matching route last, and wrapping all the <Routes/>
with a <Switch/>
, so it only renders the first hit.
Check out the docs if you have any more questions: https://reacttraining.com/react-router/core/api/Switch
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
/create
matches /:id
, so it makes sense that this route matches. I recommend forcing :id
to look for numeric only:
<Route exact path={`/admin/caters/:id(\d+)`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
Likewise, you can follow @jabsatz's recommendation, use a switch, and have it match the first route that matches. In this case, you would need to ensure that the /admin/caters/create
route is the first <Route />
element matched.
I had no idea you could do(\d+)
. That's super cool.
– David
Nov 8 at 18:21
1
@David react-router under the hood uses path-to-regexp. They explain it a bit in the docs.
– FrankerZ
Nov 8 at 18:23
@FrankerZ thank you! This fixed it for me. Didn't know about the (\d+) either.
– Roi
Nov 8 at 18:25
add a comment |
up vote
4
down vote
accepted
/create
matches /:id
, so it makes sense that this route matches. I recommend forcing :id
to look for numeric only:
<Route exact path={`/admin/caters/:id(\d+)`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
Likewise, you can follow @jabsatz's recommendation, use a switch, and have it match the first route that matches. In this case, you would need to ensure that the /admin/caters/create
route is the first <Route />
element matched.
I had no idea you could do(\d+)
. That's super cool.
– David
Nov 8 at 18:21
1
@David react-router under the hood uses path-to-regexp. They explain it a bit in the docs.
– FrankerZ
Nov 8 at 18:23
@FrankerZ thank you! This fixed it for me. Didn't know about the (\d+) either.
– Roi
Nov 8 at 18:25
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
/create
matches /:id
, so it makes sense that this route matches. I recommend forcing :id
to look for numeric only:
<Route exact path={`/admin/caters/:id(\d+)`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
Likewise, you can follow @jabsatz's recommendation, use a switch, and have it match the first route that matches. In this case, you would need to ensure that the /admin/caters/create
route is the first <Route />
element matched.
/create
matches /:id
, so it makes sense that this route matches. I recommend forcing :id
to look for numeric only:
<Route exact path={`/admin/caters/:id(\d+)`} component={Cater} />
<Route exact path={'/admin/caters/create'} component={CreateCater} />
Likewise, you can follow @jabsatz's recommendation, use a switch, and have it match the first route that matches. In this case, you would need to ensure that the /admin/caters/create
route is the first <Route />
element matched.
answered Nov 8 at 18:14
FrankerZ
15.7k72859
15.7k72859
I had no idea you could do(\d+)
. That's super cool.
– David
Nov 8 at 18:21
1
@David react-router under the hood uses path-to-regexp. They explain it a bit in the docs.
– FrankerZ
Nov 8 at 18:23
@FrankerZ thank you! This fixed it for me. Didn't know about the (\d+) either.
– Roi
Nov 8 at 18:25
add a comment |
I had no idea you could do(\d+)
. That's super cool.
– David
Nov 8 at 18:21
1
@David react-router under the hood uses path-to-regexp. They explain it a bit in the docs.
– FrankerZ
Nov 8 at 18:23
@FrankerZ thank you! This fixed it for me. Didn't know about the (\d+) either.
– Roi
Nov 8 at 18:25
I had no idea you could do
(\d+)
. That's super cool.– David
Nov 8 at 18:21
I had no idea you could do
(\d+)
. That's super cool.– David
Nov 8 at 18:21
1
1
@David react-router under the hood uses path-to-regexp. They explain it a bit in the docs.
– FrankerZ
Nov 8 at 18:23
@David react-router under the hood uses path-to-regexp. They explain it a bit in the docs.
– FrankerZ
Nov 8 at 18:23
@FrankerZ thank you! This fixed it for me. Didn't know about the (\d+) either.
– Roi
Nov 8 at 18:25
@FrankerZ thank you! This fixed it for me. Didn't know about the (\d+) either.
– Roi
Nov 8 at 18:25
add a comment |
up vote
1
down vote
The problem is that :id
is matching with create
(so, it thinks "see cater with id create
"). The way to solve this is to put the wildcard matching route last, and wrapping all the <Routes/>
with a <Switch/>
, so it only renders the first hit.
Check out the docs if you have any more questions: https://reacttraining.com/react-router/core/api/Switch
add a comment |
up vote
1
down vote
The problem is that :id
is matching with create
(so, it thinks "see cater with id create
"). The way to solve this is to put the wildcard matching route last, and wrapping all the <Routes/>
with a <Switch/>
, so it only renders the first hit.
Check out the docs if you have any more questions: https://reacttraining.com/react-router/core/api/Switch
add a comment |
up vote
1
down vote
up vote
1
down vote
The problem is that :id
is matching with create
(so, it thinks "see cater with id create
"). The way to solve this is to put the wildcard matching route last, and wrapping all the <Routes/>
with a <Switch/>
, so it only renders the first hit.
Check out the docs if you have any more questions: https://reacttraining.com/react-router/core/api/Switch
The problem is that :id
is matching with create
(so, it thinks "see cater with id create
"). The way to solve this is to put the wildcard matching route last, and wrapping all the <Routes/>
with a <Switch/>
, so it only renders the first hit.
Check out the docs if you have any more questions: https://reacttraining.com/react-router/core/api/Switch
answered Nov 8 at 18:14
jabsatz
8617
8617
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53213614%2freact-router-v4-rendering-two-components-on-same-route%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
Try swapping the
<Route />
tags (Put /create first), and let me know if that changes anything– FrankerZ
Nov 8 at 18:04
Thank you for the reply @FrankerZ. I tried it and it didn't make any difference. I tried making url's different in length, so that create was looking like
/admin/cater/create/cater
. And then I didn't have any issues, but that is not how I want to structure my URL– Roi
Nov 8 at 18:12