Skip to content

Commit 53a68c6

Browse files
committed
chore(docs): Update building with components
1 parent a259ff8 commit 53a68c6

File tree

1 file changed

+25
-96
lines changed

1 file changed

+25
-96
lines changed

docs/docs/conceptual/building-with-components.md

+25-96
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ title: Building with Components
44

55
To use Gatsby, you will need a basic understanding of React components.
66

7-
The [official tutorial](https://reactjs.org/tutorial/tutorial.html)
8-
is a good place to start.
7+
The [official React tutorial](https://reactjs.org/tutorial/tutorial.html) is a good place to start.
98

109
## Why React components?
1110

12-
React's component architecture simplifies building large websites by encouraging
13-
modularity, reusability, and clear abstractions. React has a large ecosystem of
14-
open source components, tutorials, and tooling that can be used seamlessly for
15-
building sites with Gatsby. Gatsby is built to behave almost exactly like a
16-
normal React application.
11+
React's component architecture simplifies building large websites by encouraging modularity, reusability, and clear abstractions. React has a large ecosystem of open source components, tutorials, and tooling that can be used seamlessly for building sites with Gatsby. Gatsby is built to behave almost exactly like a normal React application.
1712

1813
The following model shows how data from a source can be queried by GraphQL for use inside components in the process of building a Gatsby site:
1914

@@ -30,37 +25,35 @@ A basic directory structure of a project might look like this:
3025

3126
```text
3227
.
28+
├── posts
29+
│  ├── 01-01-2017
30+
│  │   └── index.md
31+
│  ├── 01-02-2017
32+
│  │   └── index.md
33+
│   └── 01-03-2017
34+
│   └── index.md
3335
├── gatsby-config.js
3436
├── package.json
3537
└── src
36-
   ├── html.js
38+
   ├── components
39+
   │ └── seo.jsx
3740
   ├── pages
38-
   │   ├── index.js
39-
   │   └── posts
40-
   │      ├── 01-01-2017
41-
   │      │   └── index.md
42-
   │      ├── 01-02-2017
43-
   │      │   └── index.md
44-
   │      └── 01-03-2017
45-
   │         └── index.md
46-
   ├── templates
47-
   └── post.js
41+
   │   ├── index.jsx
42+
   │   └── about.jsx
43+
   └── templates
44+
   └── post.jsx
4845
```
4946

5047
### Page components
5148

52-
Components under `src/pages` become pages automatically with paths based on
53-
their file name. For example `src/pages/index.js` is mapped to `yoursite.com`
54-
and `src/pages/about.js` becomes `yoursite.com/about/`. Every `.js` or `.jsx`
55-
file in the pages directory must resolve to either a string or react component,
56-
otherwise your build will fail.
49+
Components under `src/pages` become pages automatically with paths based on their file name. For example `src/pages/index.jsx` is mapped to `yoursite.com` and `src/pages/about.jsx` becomes `yoursite.com/about/`. Every `.js`, `.jsx`, or `.tsx` file in the pages directory must resolve to a React component, otherwise your build will fail.
5750

5851
Example:
5952

60-
```jsx:title=src/pages/about.js
61-
import React from "react"
53+
```jsx:title=src/pages/about.jsx
54+
import * as React from "react"
6255

63-
function AboutPage(props) {
56+
function AboutPage() {
6457
return (
6558
<div className="about-container">
6659
<p>About me.</p>
@@ -73,19 +66,16 @@ export default AboutPage
7366

7467
### Page template components
7568

76-
You can programmatically create pages using "page template components". All
77-
pages are React components but very often these components are just wrappers around data from files or other sources.
69+
You can programmatically create pages using "page template components". All pages are React components but very often these components are just wrappers around data from files or other sources.
7870

79-
`src/templates/post.js` is an example of a page component. It queries GraphQL
80-
for markdown data and then renders the page using this data.
71+
`src/templates/post.jsx` is an example of a page template. It queries GraphQL for markdown data (sourcing from the `posts` directory) and then renders the page using this data.
8172

82-
See [part six](/docs/tutorial/part-6/) of the tutorial for a detailed
83-
introduction to programmatically creating pages.
73+
See [part six](/docs/tutorial/part-6/) of the tutorial for a detailed introduction to programmatically creating pages.
8474

8575
Example:
8676

87-
```jsx:title=src/templates/post.js
88-
import React from "react"
77+
```jsx:title=src/templates/post.jsx
78+
import * as React from "react"
8979
import { graphql } from "gatsby"
9080

9181
function BlogPostTemplate(props) {
@@ -112,67 +102,6 @@ export const pageQuery = graphql`
112102
`
113103
```
114104

115-
### HTML component
116-
117-
`src/html.js` is responsible for everything other than where Gatsby lives in
118-
the `<body />`.
119-
120-
In this file, you can modify the `<head>` metadata and general structure of the
121-
document and add external links.
122-
123-
Typically you should omit this from your site as the default `html.js` file will
124-
suffice. If you need more control over server rendering, then it's valuable to
125-
have an `html.js`.
126-
127-
Example:
128-
129-
```jsx:title=src/html.js
130-
import React from "react"
131-
import favicon from "./favicon.png"
132-
133-
let inlinedStyles = ""
134-
if (process.env.NODE_ENV === "production") {
135-
try {
136-
inlinedStyles = require("!raw-loader!../public/styles.css")
137-
} catch (e) {
138-
console.log(e)
139-
}
140-
}
141-
142-
function HTML(props) {
143-
let css
144-
if (process.env.NODE_ENV === "production") {
145-
css = (
146-
<style
147-
id="gatsby-inlined-css"
148-
dangerouslySetInnerHTML={{ __html: inlinedStyles }}
149-
/>
150-
)
151-
}
152-
return (
153-
<html lang="en">
154-
<head>
155-
<meta charSet="utf-8" />
156-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
157-
{props.headComponents}
158-
<link rel="icon" href={favicon} />
159-
{css}
160-
</head>
161-
<body>
162-
<div id="___gatsby" dangerouslySetInnerHTML={{ __html: props.body }} />
163-
{props.postBodyComponents}
164-
</body>
165-
</html>
166-
)
167-
}
168-
```
169-
170-
These are examples of the different ways React components are used in Gatsby
171-
sites. To see full working examples, check out the
172-
[examples directory](https://github.com/gatsbyjs/gatsby/tree/master/examples) in
173-
the Gatsby repo.
174-
175105
### Non-page components
176106

177-
A Non-page component is one that's embedded inside some other component, forming a component hierarchy. An example would be a Header component that's included in multiple page components.
178-
Gatsby uses GraphQL to enable components to declare the data they need. Using the [useStaticQuery hook](/docs/how-to/querying-data/use-static-query/), you can colocate a non-page component with its data.
107+
A non-page component is one that's embedded inside some other component, forming a component hierarchy. An example would be a SEO component that's included in multiple page components. Gatsby uses GraphQL to enable components to declare the data they need. Using the [useStaticQuery hook](/docs/how-to/querying-data/use-static-query/), you can colocate a non-page component with its data. In the example above that would be the `src/components/seo.jsx` component. Here's a guide on how to [create a SEO component](/docs/how-to/adding-common-features/adding-seo-component/).

0 commit comments

Comments
 (0)