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 :(










share|improve this question






















  • 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

















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 :(










share|improve this question






















  • 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















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 :(










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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




















  • 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














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.






share|improve this answer





















  • 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


















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






share|improve this answer





















    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',
    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
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer





















    • 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















    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.






    share|improve this answer





















    • 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













    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.






    share|improve this answer












    /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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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












    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






    share|improve this answer

























      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






      share|improve this answer























        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 18:14









        jabsatz

        8617




        8617






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()