{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/routes-and-views","result":{"data":{"mdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Routes and Views\",\n  \"order\": 5,\n  \"free\": true,\n  \"active\": true,\n  \"tech\": [\"react\", \"react-router\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Routes and views\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"React components\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"React router\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Hash history\")), mdx(\"p\", null, \"In this section, we set up our router using \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"react-router\"), \", which has become the default router for React. We're also going to create a few basic views so that our router has something to display.\"), mdx(\"p\", null, \"We're also going to go some of the very basics of how routers work and the various routing options. For the time being, we're going to stick to a hash router (explained below).\"), mdx(\"h2\", null, \"Components\"), mdx(\"p\", null, \"The first thing we should do is create a few components for our router to render. For now, let's create a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \" component for our root route (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/\"), \") and a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Settings\"), \" component for our user profile and settings (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/settings\"), \").\"), mdx(\"p\", null, \"Because React allows us to reuse components, we should be as explicit as possible with our component names. At some point in the future, we will have a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Message\"), \" component that will be used once for every message in a chat between customer and representative, and may even be shared across applications.\"), mdx(\"p\", null, \"For the sake of clarity, we will avoid using plural names whenever possible. \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Comment\"), \" vs. \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Comments\"), \" will always lead to confusion down the road. As we add more components, it will become abundantly clear why this is necessary.\"), mdx(\"p\", null, \"We will want to keep all of these organized in a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"components/\"), \" directory under \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"app/\"), \". Let's create that now along with directories and files for each of our new components. \", mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"Remember, directory changes may not be included in the remainder of this course for the sake of brevity.\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ mkdir -p app/components/{Home,Settings}\\n$ touch app/components/{Home/index.js,Settings/index.js}\\n\")), mdx(\"p\", null, \"Note the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"-p\"), \" flag in our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mkdir\"), \" command. This tells bash that if the directory does not exist, go ahead and make it. If we did not use this, we would have to run \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mkdir app/components\"), \" first.\"), mdx(\"p\", null, \"And while we're at it, let's create some generic content in each of those \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"index.js\"), \" files we just created.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\n\\nexport class Home extends React.Component {\\n  render() {\\n    return <div>Home component</div>\\n  }\\n}\\n\\nexport default Home\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\n\\nexport class Settings extends React.Component {\\n  render() {\\n    return <div>Settings component</div>\\n  }\\n}\\n\\nexport default Settings\\n\")), mdx(\"h2\", null, \"React Router\"), mdx(\"p\", null, \"We are going to use the popular \", mdx(\"a\", {\n    \"href\": \"https://github.com/rackt/react-router\",\n    \"parentName\": \"p\"\n  }, \"React Router\"), \" for our routing. If you are not familiar with what routing is, it's the thing that handles your url path, such as \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"http://github.com/learnphoenix\"), \", and directs you to the proper view. Without a router, your app will be limited to a single page (this is not strictly true as there are ways around using a router, but this is \", mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"generally\"), \" true).\"), mdx(\"p\", null, \"Now that we have a few components set up, we should direct our router to access them. In order to use React Router, we need to add a few dependencies.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ npm install --save history react-router\\n\")), mdx(\"p\", null, \"React Router requires the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"history\"), \" package and does not automatically install it for you.\"), mdx(\"p\", null, \"The next thing we need to do is change the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"index.js\"), \" file within our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"app\"), \" directory to use our router. Rather than use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ReactDOM\"), \" to directly render directly to the DOM, we're going to go through React Router to render the appropriate content based on the current path.\"), mdx(\"p\", null, \"Add the following code the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"index.js\"), \" file at the root of your project. Each line will be described in detail after the code block.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport ReactDOM from \\\"react-dom\\\"\\nimport { Router, Route, IndexRoute, hashHistory } from \\\"react-router\\\"\\n\\nimport { default as Home } from \\\"./components/Home\\\"\\nimport { default as Settings } from \\\"./components/Settings\\\"\\n\\nconst App = props => <div>{props.children}</div>\\n\\nReactDOM.render(\\n  <Router history={hashHistory}>\\n    <Route path=\\\"/\\\" component={App}>\\n      <IndexRoute component={Home} />\\n      <Route path=\\\"settings\\\" component={Settings} />\\n    </Route>\\n  </Router>,\\n  document.getElementById(\\\"root\\\")\\n)\\n\")), mdx(\"p\", null, \"The first thing we have here is a list of our dependencies for this file. \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"React\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"ReactDOM\"), \" should look familiar. From \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"react-router\"), \", we need \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Router\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Route\"), \", and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"IndexRoute\"), \". The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Router\"), \" is the outer element that controls our routes, \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Route\"), \" is each individual route, and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"IndexRoute\"), \" displays a component when you get to the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/\"), \" root route.\"), mdx(\"p\", null, \"Also, since we're exporting both a named export (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"export class Home ...\"), \") and a default export (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"export default Home\"), \"), we are specifying that we are importing the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"default\"), \" import using the syntax \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"{ default as ... }\"), \". This is not strictly necessary, and your app will not throw an error, but it's the recommended syntax as of July 2016.\"), mdx(\"p\", null, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"A note on ES2016 arrow function syntax:\"), \" We've also (temporarily) changed around our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"App\"), \" component to show of the different ways in which you can write functions in ES2015. When writing arrow functions in ES2015:\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"parentheses are optional when there is only one argument (in this case \\\"props\\\" is the only argument)\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"if the function is only one line, it is implicitly returned (you do not need to type out \\\"return\\\")\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"curly braces are optional for single-line functions\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"you can \", mdx(\"a\", {\n    \"href\": \"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment\",\n    \"parentName\": \"li\"\n  }, \"destructure\"), \" the arguments.\")), mdx(\"p\", null, \"In other words, all the following are equivalent:\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"const App = function (props) {\\n  return <div>{props.children}</div>\\n}.bind(this)\\n\\nconst App = props => {\\n  return <div>{props.children}</div>\\n}\\n\\nconst App = props => {\\n  return <div>{props.children}</div>\\n}\\n\\nconst App = props => <div>{props.children}</div>\\n\\nconst App = ({ children }) => <div>{children}</div>\\n\")), mdx(\"p\", null, \"The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"hashHistory\"), \" bit is something that React Router is using to create URLs. It does not require server-side configuration, so it is ideal for single page apps like this one. Eventually we might want to change this over to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"browserHistory\"), \", but we will hold off on that until we deal with server-side rendering and other options that require us to run our frontend through a server.\"), mdx(\"p\", null, \"Then we import the three components we just made so they can be rendered by the router when the user hits the relevant url.\"), mdx(\"p\", null, \"The next thing we need to do is create a an \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"App\"), \" component that holds all of our other components. Anything in this component will be rendered in every view, so use it sparingly. Within this component, we render all of the child components as we define in our router below.\"), mdx(\"p\", null, \"The last thing we need to do is set up our routes. You should be able to see a pretty obvious hierarchy of routes, with the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"App\"), \" component holding each of two child components(\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Settings\"), \") each of which has its own path.\"), mdx(\"p\", null, \"So now, if you go to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"localhost:3000\"), \", you should see your Home component. If you go to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"localhost:3000/#/settings\"), \", you should see your settings component.\"), mdx(\"h2\", null, \"Hash history\"), mdx(\"p\", null, \"There are a few things to note about \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"hashHistory\"), \". Your routes will all have a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"#\"), \" in them. So if you want to view the settings, it will be accessible at \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"localhost:3000/#/settings\"), \" rather than just \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"localhost:3000/settings\"), \".\"), mdx(\"p\", null, \"What's actually happening under the hood is there is an event listener that listens for a change in the url, and whenever it changes, it triggers a function. When you enter a url, your server ignores anything that comes after a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"#\"), \" by default. This is because data after a hash (referred to as a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"fragment\"), \") is generally used to change things only on the client; it is often used to set scroll position.\"), mdx(\"p\", null, \"You can try this out if you want by typing in \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"https://github.com/#learnphoenix\"), \". You'll notice that what you actually see is the GitHub homepage. This is because when you make a request to a particular url (e.g. \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"https://github.com/learnphoenix\"), \"), you're sending a message to the server that says, \\\"give me the data I need for \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"github.com\"), \" from the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"/learnphoenix\"), \" route\\\", but when you use a hash router, the server is only asked for \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"github.com\"), \" and the client handles the routing for everything after the hash (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"#\"), \") using \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"react-router\"), \".\"), mdx(\"p\", null, \"There is also some random stuff that gets put on the end of your url, such as \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"_k=vm23gw\"), \". This is for people who are using an older browser that does not support the HTML5 push API. This is not strictly necessary and can be disabled if you are not worried about supporting old browsers.\"), mdx(\"p\", null, \"It's actually really easy to create your own basic router. All you have to do is listen for changes in your url path and run a function that renders a new view when a particular route is hit. There's nothing magical about routers; try running \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"window.location\"), \" in your browser console to see what you have access to. That said, there are many additional features and edge cases that make using something like \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"react-router\"), \" more practical.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Routes and views React components React router Hash history In this section, we set up our router using  react-router , which has become the…","frontmatter":{"title":"Routes and Views","order":5,"active":true,"free":true,"tech":["react","react-router"]}}},"pageContext":{"slug":"routes-and-views","prevNode":{"frontmatter":{"title":"Basics of Webpack","active":true,"free":true,"order":4,"tech":["react","webpack"]},"fields":{"slug":"basics-of-webpack"}},"nextNode":{"frontmatter":{"title":"Set Up Styles in Webpack","active":true,"free":true,"order":6,"tech":["react","webpack"]},"fields":{"slug":"set-up-styles-in-webpack"}}}},"staticQueryHashes":["1723017916"]}