My Docs
My BlogPython Data StructuresRepoFamily-Promise-Docs
Web-Dev-Hub-Docs
Web-Dev-Hub-Docs
  • Home
  • Navigation
  • Tools
    • Tools
      • Docker
      • G-Cloud & Firebase
      • Heroku
      • Dropbox
      • Email
      • Tools
      • DuckDuckGo
      • Elasticsearch
      • CodeSandbox
      • Product Hunt
      • Raycast
      • Elasticsearch
      • Tools
      • Showcase
        • Utilities
        • Continued
          • Page 3:
    • Downloads
    • REPL.IT Compilations
      • Part 2
    • Past Notes
      • Command Line Reference
    • Git
    • JavaScript
      • Interview Problems
      • General JavaScript Notes
      • jQuery
      • NodeJS
        • Node argv
        • NPM
        • Express
          • ExpressJS Overview
          • Sequelize
      • Regex
      • React
        • React Examples:
        • Redux
        • Redux Docs
          • Redux Resources
        • React Notes
    • My Bash Commands
    • Learning
  • Programming Languages
    • Programming Languages
      • File System
      • Basics
      • PSEUDO Programing Languages
        • HTML
      • CSS
      • List Of Programming Languages By Type
      • Tools-Of-The-Trade
        • Linux
        • Docker
      • Git
      • Python
        • Pydrive:
        • Practice
        • Pipenv
        • Untitled
      • Bash
        • SED
      • CHEATSHEETS
      • Java
      • Html
      • Markdown
      • CSS
      • SCSS
      • C & C++
      • Ruby
      • SQL
        • PostgreSQL
      • Jest
      • JavaScript
      • Typescript
      • C++
      • Babel
    • What is a Programming Language?
  • Python
    • Python
      • Python General Notes
      • Python Quiz
      • Python Cheat Sheet
      • Python Snippets
      • Python at length
    • Install PIP
  • JavaScript
    • JavaScript
      • Jquery
      • Page 16
    • Writing Files
    • JS-Leetcode
  • Web Development Frameworks & Libraries
    • GRAPHQL
    • React
    • Jquery
      • Prac
    • GATSBY
      • Untitled
      • Building with Components
      • Plugins, Themes, & Starters
      • GraphQL Concepts
  • Productivity
    • Productivity
      • Awesome Productivity Tools for Back-to-School
  • Misc
    • Misc
      • Experiments
  • GitGateway
    • Links
    • Bookmarks
  • Websites
    • Websites
    • Not My Websites:
    • Articles
  • Backend
    • Backend
  • Networking
    • Networks
  • Resources
    • Web Dev Tutorials
      • Node
        • API Security
    • Resources
    • Video Resources
  • General Knowledge
    • General Knowledge
    • Glossary
    • Knowledge Bank
  • Finance
    • Finance
    • Finance Reference
    • Financial Trends
  • Science & Tech (Innovation)
    • Science & Tech
    • Articles
  • Reading
    • Reading
  • Social Media & Trends
    • Trends In Web Dev
    • Analytics
    • FB-Dev-Open Source
      • Content Publishing
    • IG-API
  • Docs
    • Docs
      • NodeJS
        • installed it?
        • Timers in Node.js and beyond
        • Node.js web app
        • Overview of Blocking vs Non-Blocking
        • Don't Block the Event Loop (or the Worker Pool)
  • Code Editors & Tools
    • Vscode
      • Vscode Docs
      • How To Speed Up Vscode
  • Cool Stuff
    • Cool Observable Notebooks
  • Server-Side
    • GraphQL
      • Intro
    • Rest VS GraphQl
    • REST-API
    • Public APIs
  • WEB_DEV_TOOLS
    • Web Dev Tools
    • Cloudinary
    • Postman
      • Creating an API
      • Trouble Shooting Postman
    • Netlify
      • Facebook Graph API
      • Pandoc
      • Graph API
      • Troubleshooting
      • Examples
      • HTTPS (SSL)
      • Open Authoring
      • Netlify CMS
      • Git Gateway
  • DS_ALGOS_BRAINTEASERS
    • A Quick Guide to Big-O Notation, Memoization, Tabulation, and Sorting Algorithms by Example
  • Free-Stuff
    • Free Stuff
  • Job-Search
    • Job Search
    • Outreach
  • General Comp Sci
    • Principles behind the Agile Manifesto
  • Blockchain & Crypto
    • Blockchain Basics
      • Basics:
  • Data Structures & Interviewing
    • Data Structures
    • Computational Complexity
  • REACT_REVISITED
    • Modern React with Redux
      • React-Projects
  • WEBDEV-Bootcamp-Notes
    • 🏫Lambda
      • 1.1 - User Interface and Git
      • Page 2
      • Page 1
      • Page 3
      • Page 4
      • Page 5
      • Page 6
      • Page 7
      • Page 8
      • Page 9
      • Page 10
      • Page 11
      • Page 12
      • Page 13
      • Page 14
      • Page 15
      • CS-Python-Notes
        • Python
  • Unsorted-Notes
    • Compiled-Random-Notes
    • Testing:
      • Configure Jest for Testing JavaScript Applications
      • install, configure, and script Cypress for JavaScript web applications
      • Test React Components with Jest and `react-testing-library`
      • Use testing library to evaluate any framework...
  • Medium-articles
    • My Articles
      • Python For JS Developers
      • JavaScript Programmer
      • Awesome Web Development Youtube Video Archive
      • Bash Commands That Save Me Time and Frustration
      • Git-Tricks
      • scrap
      • Medium Article
      • Everything You Need To Know About Relational Databases, SQL, PostgreSQL and Sequelize To Build…
      • Machine Learner
      • Here’s the expanded list:
      • The Complete JavaScript Reference Guide
      • This is really cool!
      • Web Development Interview Part 3💻
      • Mutability And Reference VS Privative Types in JavaScript
      • React
      • Super Simple Intro To HTML
      • Introduction to React for Complete Beginners
      • Web Developer Resource List Part 2
      • Front End Interview Questions Part 2
      • A List Of Tools For Improvement
      • Github Repositories That Will Teach You How To Code For Free!
      • Libraries
      • Machine Learner
      • Here’s the expanded list:
      • The Complete JavaScript Reference Guide
  • 🖲️AI
    • Pytorch
      • Documentation
  • 🎙️Audio
    • Audio
Powered by GitBook
On this page
  • Building with Components
  • TABLE OF CONTENTS
  • Why React components?
  • How does Gatsby use React Components?

Was this helpful?

  1. Web Development Frameworks & Libraries
  2. GATSBY

Building with Components

PreviousUntitledNextPlugins, Themes, & Starters

Last updated 3 years ago

Was this helpful?

Building with Components

TABLE OF CONTENTS

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

The is a good place to start.

Why React components?

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.

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:ContentBuildDataViewAppsrc/pages/homepage.js

export ({ data }) => (
  <div>
    <h1>{data.site.title}</h1>
    {data.site.description}
  </div>
)

Your components can pull in whatever data they need from any source in the data layer.

How does Gatsby use React Components?

Everything in Gatsby is built using components.

A basic directory structure of a project might look like this:

Copycopy code to clipboard.├── gatsby-config.js├── package.json└── src    ├── html.js    ├── pages    │   ├── index.js    │   └── posts    │       ├── 01-01-2017    │       │   └── index.md    │       ├── 01-02-2017    │       │   └── index.md    │       └── 01-03-2017    │           └── index.md    ├── templates        └── post.js

Page components

Components under src/pages become pages automatically with paths based on their file name. For example src/pages/index.js is mapped to yoursite.com and src/pages/about.js becomes yoursite.com/about/. Every .js or .jsx file in the pages directory must resolve to either a string or react component, otherwise your build will fail.

Example:src/pages/about.js

Copysrc/pages/about.js: copy code to clipboardimport React from "react"function AboutPage(props) {  return (    <div className="about-container">      <p>About me.</p>    </div>  )}export default AboutPage

Page template components

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.

src/templates/post.js is an example of a page component. It queries GraphQL for markdown data and then renders the page using this data.

Example:src/templates/post.js

Copysrc/templates/post.js: copy code to clipboardimport React from "react"import { graphql } from "gatsby"function BlogPostTemplate(props) {  const post = props.data.markdownRemark  return (    <div>      <h1>{post.frontmatter.title}</h1>      <div dangerouslySetInnerHTML={{ __html: post.html }} />    </div>  )}export default BlogPostTemplateexport const pageQuery = graphql`  query($slug: String!) {    markdownRemark(fields: { slug: { eq: $slug } }) {      html      frontmatter {        title      }    }  }`

HTML component

src/html.js is responsible for everything other than where Gatsby lives in the <body />.

In this file, you can modify the <head> metadata and general structure of the document and add external links.

Typically you should omit this from your site as the default html.js file will suffice. If you need more control over server rendering, then it’s valuable to have an html.js.

Example:src/html.js

Copysrc/html.js: copy code to clipboardimport React from "react"import favicon from "./favicon.png"let inlinedStyles = ""if (process.env.NODE_ENV === "production") {  try {    inlinedStyles = require("!raw-loader!../public/styles.css")  } catch (e) {    console.log(e)  }}function HTML(props) {  let css  if (process.env.NODE_ENV === "production") {    css = (      <style        id="gatsby-inlined-css"        dangerouslySetInnerHTML={{ __html: inlinedStyles }}      />    )  }  return (    <html lang="en">      <head>        <meta charSet="utf-8" />        <meta name="viewport" content="width=device-width, initial-scale=1.0" />        {props.headComponents}        <link rel="icon" href={favicon} />        {css}      </head>      <body>        <div id="___gatsby" dangerouslySetInnerHTML={{ __html: props.body }} />        {props.postBodyComponents}      </body>    </html>  )}

Non-page components

React powers components in Gatsby sites that are , whatever you can do in React you can do with Gatsby.

is a good resource for learning how to structure applications with React.

See of the tutorial for a detailed introduction to programmatically creating pages.

These are examples of the different ways React components are used in Gatsby sites. To see full working examples, check out the in the Gatsby repo.

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. Gatsby uses GraphQL to enable components to declare the data they need. Using the component or , you can colocate a non-page component with its data.\

Why React components?
How does Gatsby use React Components?
Page components
Page template components
HTML component
Non-page components
official tutorial
rehydrated
Thinking in React
part seven
examples directory
StaticQuery
useStaticQuery hook