You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/conceptual/building-with-components.md
+25-96
Original file line number
Diff line number
Diff line change
@@ -4,16 +4,11 @@ title: Building with Components
4
4
5
5
To use Gatsby, you will need a basic understanding of React components.
6
6
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.
9
8
10
9
## Why React components?
11
10
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.
17
12
18
13
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:
19
14
@@ -30,37 +25,35 @@ A basic directory structure of a project might look like this:
30
25
31
26
```text
32
27
.
28
+
├── posts
29
+
│ ├── 01-01-2017
30
+
│ │ └── index.md
31
+
│ ├── 01-02-2017
32
+
│ │ └── index.md
33
+
│ └── 01-03-2017
34
+
│ └── index.md
33
35
├── gatsby-config.js
34
36
├── package.json
35
37
└── src
36
-
├── html.js
38
+
├── components
39
+
│ └── seo.jsx
37
40
├── 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
48
45
```
49
46
50
47
### Page components
51
48
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.
57
50
58
51
Example:
59
52
60
-
```jsx:title=src/pages/about.js
61
-
importReactfrom"react"
53
+
```jsx:title=src/pages/about.jsx
54
+
import*asReactfrom"react"
62
55
63
-
functionAboutPage(props) {
56
+
functionAboutPage() {
64
57
return (
65
58
<div className="about-container">
66
59
<p>About me.</p>
@@ -73,19 +66,16 @@ export default AboutPage
73
66
74
67
### Page template components
75
68
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.
78
70
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.
81
72
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.
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
-
175
105
### Non-page components
176
106
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