diff --git a/packages/react-router-dom/docs/guides/basic-components.md b/packages/react-router-dom/docs/guides/basic-components.md deleted file mode 100644 index 9437ce7ef4..0000000000 --- a/packages/react-router-dom/docs/guides/basic-components.md +++ /dev/null @@ -1,117 +0,0 @@ -# Basic Components - -There are three types of components in React Router: router components, route matching components, and navigation components. - -All of the components that you use in a web application should be imported from `react-router-dom`. - -```js -import { BrowserRouter, Route, Link } from "react-router-dom"; -``` - -## Routers - -At the core of every React Router application should be a router component. For web projects, `react-router-dom` provides `` and `` routers. Both of these will create a specialized `history` object for you. Generally speaking, you should use a `` if you have a server that responds to requests and a `` if you are using a static file server. - -```jsx -import { BrowserRouter } from "react-router-dom"; -ReactDOM.render( - - - , - holder -); -``` - -## Route Matching - -There are two route matching components: `` and ``. - -```js -import { Route, Switch } from "react-router-dom"; -``` - -Route matching is done by comparing a ``'s `path` prop to the current location's `pathname`. When a `` matches it will render its content and when it does not match, it will render `null`. A `` with no path will always match. - -```jsx -// when location = { pathname: '/about' } - // renders - // renders null - // renders -``` - -You can include a `` anywhere that you want to render content based on the location. It will often make sense to list a number of possible ``s next to each other. The `` component is used to group ``s together. - -```jsx - - - - - -``` - -The `` is not required for grouping ``s, but it can be quite useful. A `` will iterate over all of its children `` elements and only render the first one that matches the current location. This helps when multiple route's paths match the same pathname, when animating transitions between routes, and in identifying when no routes match the current location (so that you can render a "404" component). - -```jsx - - - - - {/* when none of the above match, will be rendered */} - - -``` - -## Route Rendering Props - -You have three prop choices for how you render a component for a given ``: `component`, `render`, and `children`. You can check out the [`` documentation](../api/Route.md) for more information on each one, but here we'll focus on `component` and `render` because those are the two you will almost always use. - -`component` should be used when you have an existing component (either a `React.Component` or a stateless functional component) that you want to render. `render`, which takes an inline function, should only be used when you have to pass in-scope variables to the component you want to render. You should **not** use the `component` prop with an inline function to pass in-scope variables because you will get undesired component unmounts/remounts. - -```jsx -const Home = () =>
Home
; - -const App = () => { - const someVariable = true; - - return ( - - {/* these are good */} - - } - /> - {/* do not do this */} - } - /> - - ); -}; -``` - -## Navigation - -React Router provides a `` component to create links in your application. Wherever you render a ``, an anchor (``) will be rendered in your application's HTML. - -```jsx -Home -// Home -``` - -The `` is a special type of `` that can style itself as "active" when its `to` prop matches the current location. - -```jsx -// location = { pathname: '/react' } - - React - -// React -``` - -Any time that you want to force navigation, you can render a ``. When a `` renders, it will navigate using its `to` prop. - -```jsx - -``` diff --git a/packages/react-router-dom/docs/guides/primary-components.md b/packages/react-router-dom/docs/guides/primary-components.md new file mode 100644 index 0000000000..9fe29b1585 --- /dev/null +++ b/packages/react-router-dom/docs/guides/primary-components.md @@ -0,0 +1,127 @@ +# Primary Components + +There are three primary categories of components in React Router: + +- routers, like `` and `` +- route matchers, like `` and `` +- and navigation, like ``, ``, and `` + +We also like to think of the navigation components as "route changers". + +All of the components that you use in a web application should be imported from `react-router-dom`. + +```js +import { BrowserRouter, Route, Link } from "react-router-dom"; +``` + +## Routers + +At the core of every React Router application should be a router component. For web projects, `react-router-dom` provides `` and `` routers. The main difference between the two is the way they store the URL and communicate with your web server. + +- A `` uses regular URL paths. These are generally the best-looking URLs, but they require your server to be configured correctly. Specifically, your web server needs to serve the same page at all URLs that are managed client-side by React Router. Create React App supports this out of the box in development, and [comes with instructions](https://create-react-app.dev/docs/deployment#serving-apps-with-client-side-routing) on how to configure your production server as well. +- A `` stores the current location in [the `hash` portion of the URL](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/hash), so the URL looks something like `http://example.com/#/your/page`. Since the hash is never sent to the server, this means that no special server configuration is needed. + +To use a router, just make sure it is rendered at the root of your element hierarchy. Typically you'll wrap your top-level `` element in a router, like this: + +```jsx +import React from "react"; +import ReactDOM from "react-dom"; +import { BrowserRouter } from "react-router-dom"; + +function App() { + return

Hello React Router

; +} + +ReactDOM.render( + + + , + document.getElementById("root") +); +``` + +## Route Matchers + +There are two route matching components: `Switch` and `Route`. When a `` is rendered, it searches through its `children` `` elements to find one whose `path` matches the current URL. When it finds one, it renders that `` and ignores all others. This means that you should put ``s with more specific (typically longer) `path`s **before** less-specific ones. + +If no `` matches, the `` renders nothing (`null`). + +```jsx +import React from "react"; +import ReactDOM from "react-dom"; +import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; + +function App() { + return ( +
+ + {/* If the current URL is /about, this route is rendered + while the rest are ignored */} + + + + + {/* Note how these two routes are ordered. The more specific + path="/contact/:id" comes before path="/contact" so that + route will render when viewing an individual contact */} + + + + + + + + {/* If none of the previous routes render anything, + this route acts as a fallback. + + Important: A route with path="/" will *always* match + the URL because all URLs begin with a /. So that's + why we put this one last of all */} + + + + +
+ ); +} + +ReactDOM.render( + + + , + document.getElementById("root") +); +``` + +One important thing to note is that a `` matches the **beginning** of the URL, not the whole thing. So a `` will **always** match the URL. Because of this, we typically put this `` last in our ``. Another possible solution is to use `` which **does** match the entire URL. + +Note: Although React Router does support rendering `` elements outside of a ``, as of version 5.1 we recommend you use [the `useRouteMatch` hook](#TODO) instead. Additionally, we do not recommend you render a `` without a `path` and instead suggest you use [a hook](#TODO) to get access to whatever variables you need. + +## Navigation (or Route Changers) + +React Router provides a `` component to create links in your application. Wherever you render a ``, an anchor (``) will be rendered in your HTML document. + +```jsx +Home +// Home +``` + +The `` is a special type of `` that can style itself as "active" when its `to` prop matches the current location. + +```jsx + + React + + +// When the URL is /react, this renders: +// React + +// When it's something else: +// React +``` + +Any time that you want to force navigation, you can render a ``. When a `` renders, it will navigate using its `to` prop. + +```jsx + +``` diff --git a/packages/react-router-dom/docs/guides/quick-start.md b/packages/react-router-dom/docs/guides/quick-start.md index cc5b270e1f..169a5f4177 100644 --- a/packages/react-router-dom/docs/guides/quick-start.md +++ b/packages/react-router-dom/docs/guides/quick-start.md @@ -1,10 +1,8 @@ # Quick Start -You'll need a React web app to add `react-router`. +To get started with React Router in a web app, you'll need a React web app. If you need to create one, we recommend you try [Create React App](https://github.com/facebook/create-react-app). It's a popular tool that works really well with React Router. -If you need to create one, the easiest way to get started is with a popular tool called [Create React App][crapp]. - -First install `create-react-app`, if you don't already have it, and then make a new project with it. +First, install `create-react-app` and make a new project with it. ```sh npm install -g create-react-app @@ -14,25 +12,25 @@ cd demo-app ## Installation -React Router DOM is [published to npm](https://npm.im/react-router-dom) so you can install it with either `npm` or [`yarn`](https://yarnpkg.com). +You can install React Router from [the public npm registry](https://npm.im/react-router-dom) with either `npm` or [`yarn`](https://yarnpkg.com). Since we're building a web app, we'll use `react-router-dom` in this guide. ```sh npm install react-router-dom ``` -Copy/paste either of the examples (below) into your `src/App.js`. +Next, copy/paste either of the following examples into `src/App.js`. -## Example: Basic Routing +## 1st Example: Basic Routing -In this example we have 3 'Page' Components handled by the ``. +In this example we have 3 "pages" handled by the router: a home page, an about page, and a users page. As you click around on the different ``s, the router renders the matching ``. -Note: Instead of `` we use ``. +Note: Behind the scenes a `` renders an `` with a real `href`, so people using the keyboard for navigation or screen readers will still be able to use this app. ```jsx import React from "react"; -import { BrowserRouter as Router, Route, Link } from "react-router-dom"; +import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom"; -function Index() { +function Home() { return

Home

; } @@ -44,7 +42,7 @@ function Users() { return

Users

; } -function AppRouter() { +export default function App() { return (
@@ -54,46 +52,40 @@ function AppRouter() { Home
  • - About + About
  • - Users + Users
  • - - - + {/* A looks through its children s and + renders the first one that matches the current URL. */} + + + + + + + + + + +
    ); } - -export default AppRouter; ``` -## Example: Nested Routing +## 2nd Example: Nested Routing This example shows how nested routing works. The route `/topics` loads the `Topics` component, which renders any further ``'s conditionally on the paths `:id` value. ```jsx import React from "react"; -import { BrowserRouter as Router, Route, Link } from "react-router-dom"; - -function App() { - return ( - -
    -
    - - - - -
    -
    - ); -} +import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom"; function Home() { return

    Home

    ; @@ -103,10 +95,6 @@ function About() { return

    About

    ; } -function Topic({ match }) { - return

    Requested Param: {match.params.id}

    ; -} - function Topics({ match }) { return (
    @@ -121,17 +109,27 @@ function Topics({ match }) { - -

    Please select a topic.

    } - /> + {/* The Topics page has its own with more routes + that build on the /topics URL path. You can think of the + 2nd here as an "index" page for all topics, or + the page that is shown when no topic is selected */} + + + + + +

    Please select a topic.

    +
    +
    ); } -function Header() { +function Topic({ match }) { + return

    Requested topic ID: {match.params.topicId}

    ; +} + +function Navigation() { return (
    • @@ -147,9 +145,29 @@ function Header() { ); } -export default App; +export default function App() { + return ( + +
      + + + + + + + + + + + + + +
      +
      + ); +} ``` -Now you're ready to tinker. Happy routing! +## Keep Going! -[crapp]: https://github.com/facebook/create-react-app +Hopefully these examples give you a feel for what it's like to create a web app with React Router. Keep reading to learn more about [the primary components](primary-components.md) in React Router! diff --git a/website/modules/docs/Web.js b/website/modules/docs/Web.js index 397c21f850..b3ffec1528 100644 --- a/website/modules/docs/Web.js +++ b/website/modules/docs/Web.js @@ -109,13 +109,13 @@ export default { guides: [ require("../../../packages/react-router-dom/docs/guides/quick-start.md"), - require("../../../packages/react-router-dom/docs/guides/basic-components.md"), + require("../../../packages/react-router-dom/docs/guides/primary-components.md"), require("../../../packages/react-router-dom/docs/guides/server-rendering.md"), require("../../../packages/react-router-dom/docs/guides/code-splitting.md"), require("../../../packages/react-router-dom/docs/guides/scroll-restoration.md"), - require("../../../packages/react-router/docs/guides/philosophy.md"), + require("../../../packages/react-router/docs/guides/philosophy.md?web"), require("../../../packages/react-router/docs/guides/testing.md?web"), - require("../../../packages/react-router/docs/guides/redux.md"), - require("../../../packages/react-router/docs/guides/static-routes.md") + require("../../../packages/react-router/docs/guides/redux.md?web"), + require("../../../packages/react-router/docs/guides/static-routes.md?web") ] };