Create Pages
Setup Gatsby Project
bash
mkdir learn-gatsbycd learn-gatsbynpm init --y
bash
mkdir learn-gatsbycd learn-gatsbynpm init --y
mkdir
is the command to create a folder in bash command prompt. It’s equivalent to right click on File Explorer > New Folder.cd
is change directory.cd learn-gatsby
is equivalent to double click the “learn-gatsby” folder.npm init
is an utility command to start a new projects, which is actually creating apackage.json
file in current folder. By default it will ask you few questions that you need to reply one-by-one, but I am lazy and add the--y
flag to automatically answerYes
for all the questions.
You should see a package.json
created in learn-gatsby folder.
package.jsonjson
{"name": "learn-gatsby","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],"author": "","license": "ISC"}
package.jsonjson
{"name": "learn-gatsby","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],"author": "","license": "ISC"}
Now let’s install the libraries we need:
bash
npm install react react-dom gatsby
bash
npm install react react-dom gatsby
npm install
installs third-party libraries for your project. It will download the libraries and put them innode_modules
folder and add the libraries’ name and version in thepackage.json
file.
package.jsonjson
{"name": "learn-gatsby","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],"author": "","license": "ISC","dependencies": {"gatsby": "^2.13.7","react": "^16.8.6","react-dom": "^16.8.6"}}
package.jsonjson
{"name": "learn-gatsby","version": "1.0.0","description": "","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],"author": "","license": "ISC","dependencies": {"gatsby": "^2.13.7","react": "^16.8.6","react-dom": "^16.8.6"}}
Next, let’s setup our repository so we can properly record our changes and push it to GitHub.
bash
touch .gitignore
bash
touch .gitignore
.gitignoretext
.cachenode_modules/public
.gitignoretext
.cachenode_modules/public
bash
git initgit add .git commit -m "initial commit"
bash
git initgit add .git commit -m "initial commit"
Create First Page
bash
mkdir -p src/pagestouch src/pages/index.js
bash
mkdir -p src/pagestouch src/pages/index.js
src/pages/index.jsjsx
import * as React from 'react';const HomePage = () => {return (<div><h1>Welcome to Malcolm Kee's Website</h1><p>crafted with tears and love.</p></div>);};export default HomePage;
src/pages/index.jsjsx
import * as React from 'react';const HomePage = () => {return (<div><h1>Welcome to Malcolm Kee's Website</h1><p>crafted with tears and love.</p></div>);};export default HomePage;
package.jsonjson
{"name": "learn-gatsby","version": "1.0.0","description": "","main": "index.js","scripts": {"start": "gatsby develop"},"keywords": [],"author": "","license": "ISC","dependencies": {"gatsby": "^2.13.7","react": "^16.8.6","react-dom": "^16.8.6"}}
package.jsonjson
{"name": "learn-gatsby","version": "1.0.0","description": "","main": "index.js","scripts": {"start": "gatsby develop"},"keywords": [],"author": "","license": "ISC","dependencies": {"gatsby": "^2.13.7","react": "^16.8.6","react-dom": "^16.8.6"}}
bash
npm start
bash
npm start
Open localhost:8000 and you should able to see your page. Try to edit the content and see how the content is updated instantly.
bash
git add .git commit -m "add home page"
bash
git add .git commit -m "add home page"
Create Not Found Page
bash
touch src/pages/404.js
bash
touch src/pages/404.js
src/pages/404.jsjsx
import * as React from 'react';const NotFoundPage = () => {return (<div><h1>Page Not Found</h1><p>Man... I'm sorry for you</p></div>);};export default NotFoundPage;
src/pages/404.jsjsx
import * as React from 'react';const NotFoundPage = () => {return (<div><h1>Page Not Found</h1><p>Man... I'm sorry for you</p></div>);};export default NotFoundPage;
bash
git add .git commit -m "add not found page"
bash
git add .git commit -m "add not found page"
Use React Components to Compose Your Page
bash
mkdir src/componentstouch src/components/header.jsxtouch src/components/layout.jsx
bash
mkdir src/componentstouch src/components/header.jsxtouch src/components/layout.jsx
src/components/header.jsxjsx
import * as React from 'react';export const Header = () => {return (<header className="header"><div className="header-container">Malcolm Kee</div></header>);};
src/components/header.jsxjsx
import * as React from 'react';export const Header = () => {return (<header className="header"><div className="header-container">Malcolm Kee</div></header>);};
src/components/layout.jsxjsx
import * as React from 'react';import { Header } from './header';export const Layout = ({ children }) => {return (<><Header /><main className="layout-container">{children}</main></>);};
src/components/layout.jsxjsx
import * as React from 'react';import { Header } from './header';export const Layout = ({ children }) => {return (<><Header /><main className="layout-container">{children}</main></>);};
Then use the Layout
component in both our home page and not found page:
src/pages/index.jsjsx
import * as React from 'react';import { Layout } from '../components/layout';const HomePage = () => {return (<Layout><h1>Welcome to Malcolm Kee's Website</h1><p>crafted with tears and love.</p></Layout>);};export default HomePage;
src/pages/index.jsjsx
import * as React from 'react';import { Layout } from '../components/layout';const HomePage = () => {return (<Layout><h1>Welcome to Malcolm Kee's Website</h1><p>crafted with tears and love.</p></Layout>);};export default HomePage;
src/pages/404.jsjsx
import * as React from 'react';import { Layout } from '../components/layout';const NotFoundPage = () => {return (<Layout><h1>Page Not Found</h1><p>Man... I'm sorry for you</p></Layout>);};export default NotFoundPage;
src/pages/404.jsjsx
import * as React from 'react';import { Layout } from '../components/layout';const NotFoundPage = () => {return (<Layout><h1>Page Not Found</h1><p>Man... I'm sorry for you</p></Layout>);};export default NotFoundPage;
Add Styles
bash
touch src/components/header.csstouch src/components/layout.css
bash
touch src/components/header.csstouch src/components/layout.css
header.csscss
.header {background-color: rgb(102, 51, 153);color: #fff;}.header-container {font-size: 3rem;padding: 8px;margin: 0 auto;max-width: 1020px;}
header.csscss
.header {background-color: rgb(102, 51, 153);color: #fff;}.header-container {font-size: 3rem;padding: 8px;margin: 0 auto;max-width: 1020px;}
src/components/header.jsxjsx
import * as React from 'react';import './header.css';export const Header = () => {return (<header className="header"><div className="header-container">Malcolm Kee</div></header>);};
src/components/header.jsxjsx
import * as React from 'react';import './header.css';export const Header = () => {return (<header className="header"><div className="header-container">Malcolm Kee</div></header>);};
layout.csscss
.layout-container {margin: 0 auto;max-width: 1020px;}
layout.csscss
.layout-container {margin: 0 auto;max-width: 1020px;}
src/components/layout.jsxjsx
import * as React from 'react';import { Header } from './header';import './layout.css';export const Layout = ({ children }) => {return (<><Header /><main className="layout-container">{children}</main></>);};
src/components/layout.jsxjsx
import * as React from 'react';import { Header } from './header';import './layout.css';export const Layout = ({ children }) => {return (<><Header /><main className="layout-container">{children}</main></>);};
Commit Changes
bash
git add .git commit -m "add layout component"
bash
git add .git commit -m "add layout component"
Linking Pages
Let’s add third page in our site
bash
touch src/pages/about-me.js
bash
touch src/pages/about-me.js
src/pages/about-me.jsjsx
import * as React from 'react';import { Layout } from '../components/layout';const AboutMePage = () => {return (<Layout><h1>Malcolm Kee</h1><ul><li>Full-Snack Developer</li><li>KL, Malaysia</li></ul></Layout>);};export default AboutMePage;
src/pages/about-me.jsjsx
import * as React from 'react';import { Layout } from '../components/layout';const AboutMePage = () => {return (<Layout><h1>Malcolm Kee</h1><ul><li>Full-Snack Developer</li><li>KL, Malaysia</li></ul></Layout>);};export default AboutMePage;
Let’s add the link to this new page from home page:
src/pages/index.jsjsx
import { Link } from 'gatsby';import * as React from 'react';import { Layout } from '../components/layout';const HomePage = () => {return (<Layout><h1>Welcome to Malcolm Kee's Website</h1><p>crafted with tears and love.</p><Link to="/about-me">About</Link></Layout>);};export default HomePage;
src/pages/index.jsjsx
import { Link } from 'gatsby';import * as React from 'react';import { Layout } from '../components/layout';const HomePage = () => {return (<Layout><h1>Welcome to Malcolm Kee's Website</h1><p>crafted with tears and love.</p><Link to="/about-me">About</Link></Layout>);};export default HomePage;
Add the link to home page from Not Found page and About page:
src/pages/404.jsjsx
import { Link } from 'gatsby';import * as React from 'react';import { Layout } from '../components/layout';const NotFoundPage = () => {return (<Layout><h1>Page Not Found</h1><p>Man... I'm sorry for you</p><Link to="/">Home</Link></Layout>);};export default NotFoundPage;
src/pages/404.jsjsx
import { Link } from 'gatsby';import * as React from 'react';import { Layout } from '../components/layout';const NotFoundPage = () => {return (<Layout><h1>Page Not Found</h1><p>Man... I'm sorry for you</p><Link to="/">Home</Link></Layout>);};export default NotFoundPage;
src/pages/about-me.jsjsx
import { Link } from 'gatsby';import * as React from 'react';import { Layout } from '../components/layout';const AboutMePage = () => {return (<Layout><h1>Malcolm Kee</h1><ul><li>Full-Snack Developer</li><li>KL, Malaysia</li></ul><Link to="/">Home</Link></Layout>);};export default AboutMePage;
src/pages/about-me.jsjsx
import { Link } from 'gatsby';import * as React from 'react';import { Layout } from '../components/layout';const AboutMePage = () => {return (<Layout><h1>Malcolm Kee</h1><ul><li>Full-Snack Developer</li><li>KL, Malaysia</li></ul><Link to="/">Home</Link></Layout>);};export default AboutMePage;
Build and Deploy Your Gatsby Site
package.jsonjson
{"name": "learn-gatsby","version": "1.0.0","description": "","main": "index.js","scripts": {"start": "gatsby develop","build": "gatsby build","serve": "gatsby serve"},"keywords": [],"author": "","license": "ISC","dependencies": {"gatsby": "^2.13.7","react": "^16.8.6","react-dom": "^16.8.6"}}
package.jsonjson
{"name": "learn-gatsby","version": "1.0.0","description": "","main": "index.js","scripts": {"start": "gatsby develop","build": "gatsby build","serve": "gatsby serve"},"keywords": [],"author": "","license": "ISC","dependencies": {"gatsby": "^2.13.7","react": "^16.8.6","react-dom": "^16.8.6"}}
bash
npm run buildnpm run serve
bash
npm run buildnpm run serve
Then open localhost:9000 to view the optimized static files.
- push code to GitHub repo
- create account in Netlify