How does serving different pages in React works? The Next CEO of Stack OverflowNode.js on multi-core machinesHow to get the full url in Express?Express.js: how to get remote client addressHow do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)Node.js / Express.js - How does app.router work?Why use Redux over Facebook Flux?React Router with optional path parameterHow to push to History in React Router v4?Why does React routes doesn't work on server properly?How to serve index.html for React and handle routing at the same paths?

Why here is plural "We went to the movies last night."

What does this shorthand mean?

Only print output after finding pattern

Can the Reverse Gravity spell affect the Meteor Swarm spell?

What size rim is OK?

Why did we only see the N-1 starfighters in one film?

Term for the "extreme-extension" version of a straw man fallacy?

The King's new dress

Inappropriate reference requests from Journal reviewers

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

How to make a variable always equal to the result of some calculations?

Return of the Riley Riddles in Reverse

How long to clear the 'suck zone' of a turbofan after start is initiated?

How do we know the LHC results are robust?

How to be diplomatic in refusing to write code that breaches the privacy of our users

How can I quit an app using Terminal?

How should I support this large drywall patch?

What makes a siege story/plot interesting?

Is it okay to store user locations?

Was a professor correct to chastise me for writing "Prof. X" rather than "Professor X"?

Should I tutor a student who I know has cheated on their homework?

Does the Brexit deal have to be agreed by both Houses?

Return the Closest Prime Number

Opposite of a diet



How does serving different pages in React works?



The Next CEO of Stack OverflowNode.js on multi-core machinesHow to get the full url in Express?Express.js: how to get remote client addressHow do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)Node.js / Express.js - How does app.router work?Why use Redux over Facebook Flux?React Router with optional path parameterHow to push to History in React Router v4?Why does React routes doesn't work on server properly?How to serve index.html for React and handle routing at the same paths?










0















Let's say I have a react projects, and an Express server for serving my project, as follows(After building the project):



enter image description here]



This way, only the index.html is served, isn't it? If the user routes to a different page, how is that page sent to him?










share|improve this question


























    0















    Let's say I have a react projects, and an Express server for serving my project, as follows(After building the project):



    enter image description here]



    This way, only the index.html is served, isn't it? If the user routes to a different page, how is that page sent to him?










    share|improve this question
























      0












      0








      0








      Let's say I have a react projects, and an Express server for serving my project, as follows(After building the project):



      enter image description here]



      This way, only the index.html is served, isn't it? If the user routes to a different page, how is that page sent to him?










      share|improve this question














      Let's say I have a react projects, and an Express server for serving my project, as follows(After building the project):



      enter image description here]



      This way, only the index.html is served, isn't it? If the user routes to a different page, how is that page sent to him?







      node.js reactjs express web react-router






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 12:40









      ChikChakChikChak

      393314




      393314






















          4 Answers
          4






          active

          oldest

          votes


















          2














          Routing in javascript is managed by using HTML5 push state. So every time you click a link and go to another route, the browser history and push state is being used. That's the basis for routing in almost of all the single page applications.



          Until and unless you refresh the page, your request doesn't go to the server. Hence, index.html is served only once and after that the router (here the react-router) takes over and manages the routing in url using the history API of the browser.



          Hope this helps !






          share|improve this answer

























          • So basically only the index.html is served, and as SPA it renders that html to other pages without sending them?

            – ChikChak
            Mar 8 at 12:55






          • 1





            Yeah...so, depending upon the route the corresponding component gets rendered on the page which you have defined in your router.

            – Hemant Parashar
            Mar 8 at 12:57


















          1














          That is done using react-router which manages the routing using the browser's History API.



          This style of a website is called a single page application as opposed to a multi page application where the server sends different pages depending on the url you route to.






          share|improve this answer






























            1














            you can use react-router-dom like this



            <Switch>
            <Route exact path="/" component=Home/>
            <Route path="/someurl" component=ComponentForSomeUrl
            </Switch>


            and render it with BrowserRouter



            but you can use something like history.push, in my opinion react-router-dom is really simple and better than react-router



            you don't need to send html file to specific route, in case of react express is used for building API (in most cases)






            share|improve this answer






























              0














              In you React folder you want to do npm install --save react-router-dom.



              So inside the React Router family of libraries, there is a couple of different dependencies you can possibly install.



              Ensure you never install React Router by itself.



              The react-router library as it is published on npm is the core library of everything inside the React Router general project.



              So react-router has some core navigational logic inside of it. It decides how to work with React, how to change content out depending on different rules and some other low-level logic.



              To gain some actual implementation as it works specifically in the browser, install react-router-dom.



              So anytime you want to use React Router on a project to handle navigation, always install react-router-dom, not react-router.



              There are other similarly named projects that you might think you need as well, react-router-native for use inside of React Native projects.



              In web applications we make use of react-router-dom, we are not making native mobile apps.



              React-router-native is for native mobile applications only.



              For the browser you always want react-router-dom as opposed to react-router-native



              So perhaps in your App.js component you want to set something up that looks like this:



              import React from "react";
              import BrowserRouter, Route from “react-router-dom”;

              const App = () =>
              return <div>App</div>;
              ;

              export default App;


              I also recommend if you are new to React Router to get familiar with it by setting up something temporary like so:



              import React from "react";
              import BrowserRouter, Route from “react-router-dom”;

              const PageOne = () =>
              return <div>PageOne</div>;
              ;

              const PageTwo = () =>
              return <div>PageTwo<button>Click Me</button></div>;
              ;

              const App = () =>
              return (
              <div>
              <BrowserRouter>
              <div>
              <Route path=“/” exact component=PageOne />
              <Route path=“/pagetwo” component=PageTwo />
              </div>
              </BrowserRouter>
              </div>
              );
              ;


              Visit your localhost:3000 and then your localhost:3000/pagetwo, check out how all that is working.



              When we visit a page called localhost:3000 and we type that address into the url it loads up the application.



              React Router itself does not care about that entire url, instead React Router only cares about all the characters that are listed after the domain name or port definition.



              Localhost:3000 is interpreted as being localhost:3000/



              If I go to localhost:3000/ it still loads up my application.



              Now I have other examples here if I go to localhost:3000/pageone, React Router only cares about everything after the port and domain



              Same thing if I went to airbnb.com/listings/spain react router would only consider /listings/spain when deciding what content to render to the screen.



              Notice in the example above I created an instance of BrowserRouter, well BrowserRouter creates an object of its own known as the history object.



              This history object is going to look at the URL inside the address bar and extract just that portion of the URL that react router cares about.



              The history object is then going to communicate that path over to BrowserRouter who communicates that path to both Route components and those Route components decide whether to show themselves or hide themselves depending on the path that the user is visiting and the path property that it was passed when it was created.






              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',
                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%2f55063457%2fhow-does-serving-different-pages-in-react-works%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                Routing in javascript is managed by using HTML5 push state. So every time you click a link and go to another route, the browser history and push state is being used. That's the basis for routing in almost of all the single page applications.



                Until and unless you refresh the page, your request doesn't go to the server. Hence, index.html is served only once and after that the router (here the react-router) takes over and manages the routing in url using the history API of the browser.



                Hope this helps !






                share|improve this answer

























                • So basically only the index.html is served, and as SPA it renders that html to other pages without sending them?

                  – ChikChak
                  Mar 8 at 12:55






                • 1





                  Yeah...so, depending upon the route the corresponding component gets rendered on the page which you have defined in your router.

                  – Hemant Parashar
                  Mar 8 at 12:57















                2














                Routing in javascript is managed by using HTML5 push state. So every time you click a link and go to another route, the browser history and push state is being used. That's the basis for routing in almost of all the single page applications.



                Until and unless you refresh the page, your request doesn't go to the server. Hence, index.html is served only once and after that the router (here the react-router) takes over and manages the routing in url using the history API of the browser.



                Hope this helps !






                share|improve this answer

























                • So basically only the index.html is served, and as SPA it renders that html to other pages without sending them?

                  – ChikChak
                  Mar 8 at 12:55






                • 1





                  Yeah...so, depending upon the route the corresponding component gets rendered on the page which you have defined in your router.

                  – Hemant Parashar
                  Mar 8 at 12:57













                2












                2








                2







                Routing in javascript is managed by using HTML5 push state. So every time you click a link and go to another route, the browser history and push state is being used. That's the basis for routing in almost of all the single page applications.



                Until and unless you refresh the page, your request doesn't go to the server. Hence, index.html is served only once and after that the router (here the react-router) takes over and manages the routing in url using the history API of the browser.



                Hope this helps !






                share|improve this answer















                Routing in javascript is managed by using HTML5 push state. So every time you click a link and go to another route, the browser history and push state is being used. That's the basis for routing in almost of all the single page applications.



                Until and unless you refresh the page, your request doesn't go to the server. Hence, index.html is served only once and after that the router (here the react-router) takes over and manages the routing in url using the history API of the browser.



                Hope this helps !







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 8 at 13:09

























                answered Mar 8 at 12:48









                Hemant ParasharHemant Parashar

                885314




                885314












                • So basically only the index.html is served, and as SPA it renders that html to other pages without sending them?

                  – ChikChak
                  Mar 8 at 12:55






                • 1





                  Yeah...so, depending upon the route the corresponding component gets rendered on the page which you have defined in your router.

                  – Hemant Parashar
                  Mar 8 at 12:57

















                • So basically only the index.html is served, and as SPA it renders that html to other pages without sending them?

                  – ChikChak
                  Mar 8 at 12:55






                • 1





                  Yeah...so, depending upon the route the corresponding component gets rendered on the page which you have defined in your router.

                  – Hemant Parashar
                  Mar 8 at 12:57
















                So basically only the index.html is served, and as SPA it renders that html to other pages without sending them?

                – ChikChak
                Mar 8 at 12:55





                So basically only the index.html is served, and as SPA it renders that html to other pages without sending them?

                – ChikChak
                Mar 8 at 12:55




                1




                1





                Yeah...so, depending upon the route the corresponding component gets rendered on the page which you have defined in your router.

                – Hemant Parashar
                Mar 8 at 12:57





                Yeah...so, depending upon the route the corresponding component gets rendered on the page which you have defined in your router.

                – Hemant Parashar
                Mar 8 at 12:57













                1














                That is done using react-router which manages the routing using the browser's History API.



                This style of a website is called a single page application as opposed to a multi page application where the server sends different pages depending on the url you route to.






                share|improve this answer



























                  1














                  That is done using react-router which manages the routing using the browser's History API.



                  This style of a website is called a single page application as opposed to a multi page application where the server sends different pages depending on the url you route to.






                  share|improve this answer

























                    1












                    1








                    1







                    That is done using react-router which manages the routing using the browser's History API.



                    This style of a website is called a single page application as opposed to a multi page application where the server sends different pages depending on the url you route to.






                    share|improve this answer













                    That is done using react-router which manages the routing using the browser's History API.



                    This style of a website is called a single page application as opposed to a multi page application where the server sends different pages depending on the url you route to.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 8 at 12:47









                    ManavMManavM

                    1,072418




                    1,072418





















                        1














                        you can use react-router-dom like this



                        <Switch>
                        <Route exact path="/" component=Home/>
                        <Route path="/someurl" component=ComponentForSomeUrl
                        </Switch>


                        and render it with BrowserRouter



                        but you can use something like history.push, in my opinion react-router-dom is really simple and better than react-router



                        you don't need to send html file to specific route, in case of react express is used for building API (in most cases)






                        share|improve this answer



























                          1














                          you can use react-router-dom like this



                          <Switch>
                          <Route exact path="/" component=Home/>
                          <Route path="/someurl" component=ComponentForSomeUrl
                          </Switch>


                          and render it with BrowserRouter



                          but you can use something like history.push, in my opinion react-router-dom is really simple and better than react-router



                          you don't need to send html file to specific route, in case of react express is used for building API (in most cases)






                          share|improve this answer

























                            1












                            1








                            1







                            you can use react-router-dom like this



                            <Switch>
                            <Route exact path="/" component=Home/>
                            <Route path="/someurl" component=ComponentForSomeUrl
                            </Switch>


                            and render it with BrowserRouter



                            but you can use something like history.push, in my opinion react-router-dom is really simple and better than react-router



                            you don't need to send html file to specific route, in case of react express is used for building API (in most cases)






                            share|improve this answer













                            you can use react-router-dom like this



                            <Switch>
                            <Route exact path="/" component=Home/>
                            <Route path="/someurl" component=ComponentForSomeUrl
                            </Switch>


                            and render it with BrowserRouter



                            but you can use something like history.push, in my opinion react-router-dom is really simple and better than react-router



                            you don't need to send html file to specific route, in case of react express is used for building API (in most cases)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 8 at 15:18









                            iliailia

                            19112




                            19112





















                                0














                                In you React folder you want to do npm install --save react-router-dom.



                                So inside the React Router family of libraries, there is a couple of different dependencies you can possibly install.



                                Ensure you never install React Router by itself.



                                The react-router library as it is published on npm is the core library of everything inside the React Router general project.



                                So react-router has some core navigational logic inside of it. It decides how to work with React, how to change content out depending on different rules and some other low-level logic.



                                To gain some actual implementation as it works specifically in the browser, install react-router-dom.



                                So anytime you want to use React Router on a project to handle navigation, always install react-router-dom, not react-router.



                                There are other similarly named projects that you might think you need as well, react-router-native for use inside of React Native projects.



                                In web applications we make use of react-router-dom, we are not making native mobile apps.



                                React-router-native is for native mobile applications only.



                                For the browser you always want react-router-dom as opposed to react-router-native



                                So perhaps in your App.js component you want to set something up that looks like this:



                                import React from "react";
                                import BrowserRouter, Route from “react-router-dom”;

                                const App = () =>
                                return <div>App</div>;
                                ;

                                export default App;


                                I also recommend if you are new to React Router to get familiar with it by setting up something temporary like so:



                                import React from "react";
                                import BrowserRouter, Route from “react-router-dom”;

                                const PageOne = () =>
                                return <div>PageOne</div>;
                                ;

                                const PageTwo = () =>
                                return <div>PageTwo<button>Click Me</button></div>;
                                ;

                                const App = () =>
                                return (
                                <div>
                                <BrowserRouter>
                                <div>
                                <Route path=“/” exact component=PageOne />
                                <Route path=“/pagetwo” component=PageTwo />
                                </div>
                                </BrowserRouter>
                                </div>
                                );
                                ;


                                Visit your localhost:3000 and then your localhost:3000/pagetwo, check out how all that is working.



                                When we visit a page called localhost:3000 and we type that address into the url it loads up the application.



                                React Router itself does not care about that entire url, instead React Router only cares about all the characters that are listed after the domain name or port definition.



                                Localhost:3000 is interpreted as being localhost:3000/



                                If I go to localhost:3000/ it still loads up my application.



                                Now I have other examples here if I go to localhost:3000/pageone, React Router only cares about everything after the port and domain



                                Same thing if I went to airbnb.com/listings/spain react router would only consider /listings/spain when deciding what content to render to the screen.



                                Notice in the example above I created an instance of BrowserRouter, well BrowserRouter creates an object of its own known as the history object.



                                This history object is going to look at the URL inside the address bar and extract just that portion of the URL that react router cares about.



                                The history object is then going to communicate that path over to BrowserRouter who communicates that path to both Route components and those Route components decide whether to show themselves or hide themselves depending on the path that the user is visiting and the path property that it was passed when it was created.






                                share|improve this answer





























                                  0














                                  In you React folder you want to do npm install --save react-router-dom.



                                  So inside the React Router family of libraries, there is a couple of different dependencies you can possibly install.



                                  Ensure you never install React Router by itself.



                                  The react-router library as it is published on npm is the core library of everything inside the React Router general project.



                                  So react-router has some core navigational logic inside of it. It decides how to work with React, how to change content out depending on different rules and some other low-level logic.



                                  To gain some actual implementation as it works specifically in the browser, install react-router-dom.



                                  So anytime you want to use React Router on a project to handle navigation, always install react-router-dom, not react-router.



                                  There are other similarly named projects that you might think you need as well, react-router-native for use inside of React Native projects.



                                  In web applications we make use of react-router-dom, we are not making native mobile apps.



                                  React-router-native is for native mobile applications only.



                                  For the browser you always want react-router-dom as opposed to react-router-native



                                  So perhaps in your App.js component you want to set something up that looks like this:



                                  import React from "react";
                                  import BrowserRouter, Route from “react-router-dom”;

                                  const App = () =>
                                  return <div>App</div>;
                                  ;

                                  export default App;


                                  I also recommend if you are new to React Router to get familiar with it by setting up something temporary like so:



                                  import React from "react";
                                  import BrowserRouter, Route from “react-router-dom”;

                                  const PageOne = () =>
                                  return <div>PageOne</div>;
                                  ;

                                  const PageTwo = () =>
                                  return <div>PageTwo<button>Click Me</button></div>;
                                  ;

                                  const App = () =>
                                  return (
                                  <div>
                                  <BrowserRouter>
                                  <div>
                                  <Route path=“/” exact component=PageOne />
                                  <Route path=“/pagetwo” component=PageTwo />
                                  </div>
                                  </BrowserRouter>
                                  </div>
                                  );
                                  ;


                                  Visit your localhost:3000 and then your localhost:3000/pagetwo, check out how all that is working.



                                  When we visit a page called localhost:3000 and we type that address into the url it loads up the application.



                                  React Router itself does not care about that entire url, instead React Router only cares about all the characters that are listed after the domain name or port definition.



                                  Localhost:3000 is interpreted as being localhost:3000/



                                  If I go to localhost:3000/ it still loads up my application.



                                  Now I have other examples here if I go to localhost:3000/pageone, React Router only cares about everything after the port and domain



                                  Same thing if I went to airbnb.com/listings/spain react router would only consider /listings/spain when deciding what content to render to the screen.



                                  Notice in the example above I created an instance of BrowserRouter, well BrowserRouter creates an object of its own known as the history object.



                                  This history object is going to look at the URL inside the address bar and extract just that portion of the URL that react router cares about.



                                  The history object is then going to communicate that path over to BrowserRouter who communicates that path to both Route components and those Route components decide whether to show themselves or hide themselves depending on the path that the user is visiting and the path property that it was passed when it was created.






                                  share|improve this answer



























                                    0












                                    0








                                    0







                                    In you React folder you want to do npm install --save react-router-dom.



                                    So inside the React Router family of libraries, there is a couple of different dependencies you can possibly install.



                                    Ensure you never install React Router by itself.



                                    The react-router library as it is published on npm is the core library of everything inside the React Router general project.



                                    So react-router has some core navigational logic inside of it. It decides how to work with React, how to change content out depending on different rules and some other low-level logic.



                                    To gain some actual implementation as it works specifically in the browser, install react-router-dom.



                                    So anytime you want to use React Router on a project to handle navigation, always install react-router-dom, not react-router.



                                    There are other similarly named projects that you might think you need as well, react-router-native for use inside of React Native projects.



                                    In web applications we make use of react-router-dom, we are not making native mobile apps.



                                    React-router-native is for native mobile applications only.



                                    For the browser you always want react-router-dom as opposed to react-router-native



                                    So perhaps in your App.js component you want to set something up that looks like this:



                                    import React from "react";
                                    import BrowserRouter, Route from “react-router-dom”;

                                    const App = () =>
                                    return <div>App</div>;
                                    ;

                                    export default App;


                                    I also recommend if you are new to React Router to get familiar with it by setting up something temporary like so:



                                    import React from "react";
                                    import BrowserRouter, Route from “react-router-dom”;

                                    const PageOne = () =>
                                    return <div>PageOne</div>;
                                    ;

                                    const PageTwo = () =>
                                    return <div>PageTwo<button>Click Me</button></div>;
                                    ;

                                    const App = () =>
                                    return (
                                    <div>
                                    <BrowserRouter>
                                    <div>
                                    <Route path=“/” exact component=PageOne />
                                    <Route path=“/pagetwo” component=PageTwo />
                                    </div>
                                    </BrowserRouter>
                                    </div>
                                    );
                                    ;


                                    Visit your localhost:3000 and then your localhost:3000/pagetwo, check out how all that is working.



                                    When we visit a page called localhost:3000 and we type that address into the url it loads up the application.



                                    React Router itself does not care about that entire url, instead React Router only cares about all the characters that are listed after the domain name or port definition.



                                    Localhost:3000 is interpreted as being localhost:3000/



                                    If I go to localhost:3000/ it still loads up my application.



                                    Now I have other examples here if I go to localhost:3000/pageone, React Router only cares about everything after the port and domain



                                    Same thing if I went to airbnb.com/listings/spain react router would only consider /listings/spain when deciding what content to render to the screen.



                                    Notice in the example above I created an instance of BrowserRouter, well BrowserRouter creates an object of its own known as the history object.



                                    This history object is going to look at the URL inside the address bar and extract just that portion of the URL that react router cares about.



                                    The history object is then going to communicate that path over to BrowserRouter who communicates that path to both Route components and those Route components decide whether to show themselves or hide themselves depending on the path that the user is visiting and the path property that it was passed when it was created.






                                    share|improve this answer















                                    In you React folder you want to do npm install --save react-router-dom.



                                    So inside the React Router family of libraries, there is a couple of different dependencies you can possibly install.



                                    Ensure you never install React Router by itself.



                                    The react-router library as it is published on npm is the core library of everything inside the React Router general project.



                                    So react-router has some core navigational logic inside of it. It decides how to work with React, how to change content out depending on different rules and some other low-level logic.



                                    To gain some actual implementation as it works specifically in the browser, install react-router-dom.



                                    So anytime you want to use React Router on a project to handle navigation, always install react-router-dom, not react-router.



                                    There are other similarly named projects that you might think you need as well, react-router-native for use inside of React Native projects.



                                    In web applications we make use of react-router-dom, we are not making native mobile apps.



                                    React-router-native is for native mobile applications only.



                                    For the browser you always want react-router-dom as opposed to react-router-native



                                    So perhaps in your App.js component you want to set something up that looks like this:



                                    import React from "react";
                                    import BrowserRouter, Route from “react-router-dom”;

                                    const App = () =>
                                    return <div>App</div>;
                                    ;

                                    export default App;


                                    I also recommend if you are new to React Router to get familiar with it by setting up something temporary like so:



                                    import React from "react";
                                    import BrowserRouter, Route from “react-router-dom”;

                                    const PageOne = () =>
                                    return <div>PageOne</div>;
                                    ;

                                    const PageTwo = () =>
                                    return <div>PageTwo<button>Click Me</button></div>;
                                    ;

                                    const App = () =>
                                    return (
                                    <div>
                                    <BrowserRouter>
                                    <div>
                                    <Route path=“/” exact component=PageOne />
                                    <Route path=“/pagetwo” component=PageTwo />
                                    </div>
                                    </BrowserRouter>
                                    </div>
                                    );
                                    ;


                                    Visit your localhost:3000 and then your localhost:3000/pagetwo, check out how all that is working.



                                    When we visit a page called localhost:3000 and we type that address into the url it loads up the application.



                                    React Router itself does not care about that entire url, instead React Router only cares about all the characters that are listed after the domain name or port definition.



                                    Localhost:3000 is interpreted as being localhost:3000/



                                    If I go to localhost:3000/ it still loads up my application.



                                    Now I have other examples here if I go to localhost:3000/pageone, React Router only cares about everything after the port and domain



                                    Same thing if I went to airbnb.com/listings/spain react router would only consider /listings/spain when deciding what content to render to the screen.



                                    Notice in the example above I created an instance of BrowserRouter, well BrowserRouter creates an object of its own known as the history object.



                                    This history object is going to look at the URL inside the address bar and extract just that portion of the URL that react router cares about.



                                    The history object is then going to communicate that path over to BrowserRouter who communicates that path to both Route components and those Route components decide whether to show themselves or hide themselves depending on the path that the user is visiting and the path property that it was passed when it was created.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Mar 9 at 17:22

























                                    answered Mar 8 at 16:04









                                    DanielDaniel

                                    1,74121638




                                    1,74121638



























                                        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.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063457%2fhow-does-serving-different-pages-in-react-works%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

                                        How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                                        Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                                        List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229