They are preconfigured and hidden so that you can focus on the code.
If you came across gatsby you will notice that there is a lot of similarity between them. In this Blog post I will explain the key difference between the two.
What is Gatsby?
Gatsby is a blazing fast static site generator for React. Actually, it is more than that. Think of it as a PWA (Progressive Web App) Framework with best practices backed in. For example: you get code and data splitting out-of-the-box.
Why Moving to Gatsby?
Gatsby.js let's use modern web stack without the setup headache. With its flexible plugin system it let's you bring your own data source. Like Contentful, Databases or your filesystem.
Gatsby.js provides code and data splitting out-of-the-box. It loads first your critical HTML and CSS. Once that loaded it prefetches resources for other pages. That way clicking around feels so fast.
Gatsby.js uses GraphQL to share data across pages. You only get the data you need in the page. At build time Gatsby will resolve the query and embed it in your page.
Gatsby.js project folder structure
From React Routes to Gatsby Pages
There are 2 types of routes, static when you know all the part that will define your route like
/home. And dynamic when part of your route is only know at runtime like
Let's assume you have the following static routes in our create-react-app project:
In Gatsby.js, to have these routes you need to create a component with the name like the route path in the pages folder. It create the routes for you. The good news is the react components are already created so it is a matter of copying/pasting them. Except for the home page you need to name it index.js. You will end up with something like this
Now that you converted your static routes let's tackle the dynamic routes.
I will take an example of blog posts in this case loaded from Contentful. Every blog post has a uniq slug used to load its content.
In a normal react app the route will look something like this.
BlogPost component will look something like this:
To create pages dynamically in Gatsby.js you need to write some logic in the
gatsby-node.js file. To get an idea on what is possible to do at build time checkout the Gatsb.js Node.js API docs.
We will use the createPages function.
Following out Contentful example we need to create a page for each article. To do that first we need to get a list of all blog posts and create pages for them based on their uniq slug.
The code will look like this:
Since you already have the BlogPost component, form your react project. Move it to
Your Gatbsy project will look like this:
You need to make some slight modification to your Blogpost component.
$slug part that's passed through the context when creating the page to be able to use it in the GraphQL query.
Gatsby.js will pick the exported
pageQuery const and will know it's a GraphQL query string by the
From the React state to GraphQL
I will not go in depth with how to manage a React state since there is a lot of ways to achieve that. There is the new React 16 Context API or using Redux etc... Using Gatsby.js you can request the data you need using the GraphQL data layer as shown in the previous example. this option is only available in the root components. This will change in v2 using static queries feature. You can still use Redux with Gatsby.js depends on your use if GraphQL is not enough.