Nested routes and Switch - how to pass props.match.params.id?












0














I have been trying to understand nested routes and switch in the React v4 Router.
Consider the main router looks like this (simplified):



<Switch>
<Route path="/" component={LoginPage} exact={true} />
<Route path="/dashboard/edit/:id" component={DashboardPage} />
<Route path="/dashboard" component={DashboardPage} />
</Switch>


The "dashboard" component renders the sub-route:



  render(){
return (
<div className="note">
<Route to='/edit/:id' render={(props) =>
<div>
<NoteList {...props} />
<EditNotePage {...props} />
</div>
} />
</div>
)
}


The "EditNotePage" component can access the param by:



const mapStateToProps = (state, props) => ({
note: state.notes.find((note) => note.id === props.match.params.id
});


Is this the correct approach?
It seems a little redundant to specify "/dashboard/edit/:id" twice ( ? )
Once in main router and the again in the dashboard component.



However, if I do not match the route in the main router "Switch" the "props.match.params.id" is not accessible since "props.match" will only point to "/dashboard" .



Have I missed something crucial regarding how the React v4 Router works? :)



Kind regards
Kermit










share|improve this question





























    0














    I have been trying to understand nested routes and switch in the React v4 Router.
    Consider the main router looks like this (simplified):



    <Switch>
    <Route path="/" component={LoginPage} exact={true} />
    <Route path="/dashboard/edit/:id" component={DashboardPage} />
    <Route path="/dashboard" component={DashboardPage} />
    </Switch>


    The "dashboard" component renders the sub-route:



      render(){
    return (
    <div className="note">
    <Route to='/edit/:id' render={(props) =>
    <div>
    <NoteList {...props} />
    <EditNotePage {...props} />
    </div>
    } />
    </div>
    )
    }


    The "EditNotePage" component can access the param by:



    const mapStateToProps = (state, props) => ({
    note: state.notes.find((note) => note.id === props.match.params.id
    });


    Is this the correct approach?
    It seems a little redundant to specify "/dashboard/edit/:id" twice ( ? )
    Once in main router and the again in the dashboard component.



    However, if I do not match the route in the main router "Switch" the "props.match.params.id" is not accessible since "props.match" will only point to "/dashboard" .



    Have I missed something crucial regarding how the React v4 Router works? :)



    Kind regards
    Kermit










    share|improve this question



























      0












      0








      0







      I have been trying to understand nested routes and switch in the React v4 Router.
      Consider the main router looks like this (simplified):



      <Switch>
      <Route path="/" component={LoginPage} exact={true} />
      <Route path="/dashboard/edit/:id" component={DashboardPage} />
      <Route path="/dashboard" component={DashboardPage} />
      </Switch>


      The "dashboard" component renders the sub-route:



        render(){
      return (
      <div className="note">
      <Route to='/edit/:id' render={(props) =>
      <div>
      <NoteList {...props} />
      <EditNotePage {...props} />
      </div>
      } />
      </div>
      )
      }


      The "EditNotePage" component can access the param by:



      const mapStateToProps = (state, props) => ({
      note: state.notes.find((note) => note.id === props.match.params.id
      });


      Is this the correct approach?
      It seems a little redundant to specify "/dashboard/edit/:id" twice ( ? )
      Once in main router and the again in the dashboard component.



      However, if I do not match the route in the main router "Switch" the "props.match.params.id" is not accessible since "props.match" will only point to "/dashboard" .



      Have I missed something crucial regarding how the React v4 Router works? :)



      Kind regards
      Kermit










      share|improve this question















      I have been trying to understand nested routes and switch in the React v4 Router.
      Consider the main router looks like this (simplified):



      <Switch>
      <Route path="/" component={LoginPage} exact={true} />
      <Route path="/dashboard/edit/:id" component={DashboardPage} />
      <Route path="/dashboard" component={DashboardPage} />
      </Switch>


      The "dashboard" component renders the sub-route:



        render(){
      return (
      <div className="note">
      <Route to='/edit/:id' render={(props) =>
      <div>
      <NoteList {...props} />
      <EditNotePage {...props} />
      </div>
      } />
      </div>
      )
      }


      The "EditNotePage" component can access the param by:



      const mapStateToProps = (state, props) => ({
      note: state.notes.find((note) => note.id === props.match.params.id
      });


      Is this the correct approach?
      It seems a little redundant to specify "/dashboard/edit/:id" twice ( ? )
      Once in main router and the again in the dashboard component.



      However, if I do not match the route in the main router "Switch" the "props.match.params.id" is not accessible since "props.match" will only point to "/dashboard" .



      Have I missed something crucial regarding how the React v4 Router works? :)



      Kind regards
      Kermit







      javascript reactjs react-router-v4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 18:15









      chazsolo

      5,1071233




      5,1071233










      asked Nov 20 at 17:58









      Kermit

      92114




      92114
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Nope, didn't miss anything. That's how react router v4 works. You define full routes. The trick you can use is that you can grab the current path and prepend it to your "nested path".






          share|improve this answer





















          • Ok, fantastic! Can you give me a simple example of how to grab the current path and prepend it? path={${match.url}/edit/:id} ?
            – Kermit
            Nov 20 at 18:24










          • I would use location: this.props.location.pathname. Use that instead of match.url and it should work like you expect!
            – ZekeDroid
            Nov 20 at 19:07










          • Alternatively, for larger projects and for maintainability purposes, I would use a constants file and export these "base paths". Then import them everywhere you need them. This will help you track down bugs with routing further down, if you'd like.
            – ZekeDroid
            Nov 20 at 19:08










          • Tanks a lot! That's very good advice! (^__^)
            – Kermit
            Nov 20 at 19:42











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


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398859%2fnested-routes-and-switch-how-to-pass-props-match-params-id%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









          2














          Nope, didn't miss anything. That's how react router v4 works. You define full routes. The trick you can use is that you can grab the current path and prepend it to your "nested path".






          share|improve this answer





















          • Ok, fantastic! Can you give me a simple example of how to grab the current path and prepend it? path={${match.url}/edit/:id} ?
            – Kermit
            Nov 20 at 18:24










          • I would use location: this.props.location.pathname. Use that instead of match.url and it should work like you expect!
            – ZekeDroid
            Nov 20 at 19:07










          • Alternatively, for larger projects and for maintainability purposes, I would use a constants file and export these "base paths". Then import them everywhere you need them. This will help you track down bugs with routing further down, if you'd like.
            – ZekeDroid
            Nov 20 at 19:08










          • Tanks a lot! That's very good advice! (^__^)
            – Kermit
            Nov 20 at 19:42
















          2














          Nope, didn't miss anything. That's how react router v4 works. You define full routes. The trick you can use is that you can grab the current path and prepend it to your "nested path".






          share|improve this answer





















          • Ok, fantastic! Can you give me a simple example of how to grab the current path and prepend it? path={${match.url}/edit/:id} ?
            – Kermit
            Nov 20 at 18:24










          • I would use location: this.props.location.pathname. Use that instead of match.url and it should work like you expect!
            – ZekeDroid
            Nov 20 at 19:07










          • Alternatively, for larger projects and for maintainability purposes, I would use a constants file and export these "base paths". Then import them everywhere you need them. This will help you track down bugs with routing further down, if you'd like.
            – ZekeDroid
            Nov 20 at 19:08










          • Tanks a lot! That's very good advice! (^__^)
            – Kermit
            Nov 20 at 19:42














          2












          2








          2






          Nope, didn't miss anything. That's how react router v4 works. You define full routes. The trick you can use is that you can grab the current path and prepend it to your "nested path".






          share|improve this answer












          Nope, didn't miss anything. That's how react router v4 works. You define full routes. The trick you can use is that you can grab the current path and prepend it to your "nested path".







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 18:01









          ZekeDroid

          5,54431852




          5,54431852












          • Ok, fantastic! Can you give me a simple example of how to grab the current path and prepend it? path={${match.url}/edit/:id} ?
            – Kermit
            Nov 20 at 18:24










          • I would use location: this.props.location.pathname. Use that instead of match.url and it should work like you expect!
            – ZekeDroid
            Nov 20 at 19:07










          • Alternatively, for larger projects and for maintainability purposes, I would use a constants file and export these "base paths". Then import them everywhere you need them. This will help you track down bugs with routing further down, if you'd like.
            – ZekeDroid
            Nov 20 at 19:08










          • Tanks a lot! That's very good advice! (^__^)
            – Kermit
            Nov 20 at 19:42


















          • Ok, fantastic! Can you give me a simple example of how to grab the current path and prepend it? path={${match.url}/edit/:id} ?
            – Kermit
            Nov 20 at 18:24










          • I would use location: this.props.location.pathname. Use that instead of match.url and it should work like you expect!
            – ZekeDroid
            Nov 20 at 19:07










          • Alternatively, for larger projects and for maintainability purposes, I would use a constants file and export these "base paths". Then import them everywhere you need them. This will help you track down bugs with routing further down, if you'd like.
            – ZekeDroid
            Nov 20 at 19:08










          • Tanks a lot! That's very good advice! (^__^)
            – Kermit
            Nov 20 at 19:42
















          Ok, fantastic! Can you give me a simple example of how to grab the current path and prepend it? path={${match.url}/edit/:id} ?
          – Kermit
          Nov 20 at 18:24




          Ok, fantastic! Can you give me a simple example of how to grab the current path and prepend it? path={${match.url}/edit/:id} ?
          – Kermit
          Nov 20 at 18:24












          I would use location: this.props.location.pathname. Use that instead of match.url and it should work like you expect!
          – ZekeDroid
          Nov 20 at 19:07




          I would use location: this.props.location.pathname. Use that instead of match.url and it should work like you expect!
          – ZekeDroid
          Nov 20 at 19:07












          Alternatively, for larger projects and for maintainability purposes, I would use a constants file and export these "base paths". Then import them everywhere you need them. This will help you track down bugs with routing further down, if you'd like.
          – ZekeDroid
          Nov 20 at 19:08




          Alternatively, for larger projects and for maintainability purposes, I would use a constants file and export these "base paths". Then import them everywhere you need them. This will help you track down bugs with routing further down, if you'd like.
          – ZekeDroid
          Nov 20 at 19:08












          Tanks a lot! That's very good advice! (^__^)
          – Kermit
          Nov 20 at 19:42




          Tanks a lot! That's very good advice! (^__^)
          – Kermit
          Nov 20 at 19:42


















          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%2f53398859%2fnested-routes-and-switch-how-to-pass-props-match-params-id%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







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin