Gatsby Training
  • About this training
  • Fundamentals
    • React
    • Gatsby
    • GraphQL
    • JAMStack
    • JSX
    • Webpack
  • Getting Started
    • Build your first Gatsby site
    • Project structure
    • Component structure
  • Components
    • About Components
    • Eyebrow
    • Button
    • Heading
    • Working with CSS
    • Movie Card
  • Gatsby Core Components
    • Gatsby Link
    • Gatsby Image
  • Data and GraphQL
    • Data sources
    • Connecting a data source
    • Using the GraphQL playground
  • CreatePages
    • createPages Function
    • Create a template
  • Resources
    • Gatsby and React Resources
    • Other resources
Powered by GitBook
On this page
  • Exercise:
  • Let's use the new Eyebrow component (only as an example)

Was this helpful?

  1. Components

Eyebrow

Before we dive into the more advance stuff, let's start by creating a super simple component. The component name is Eyebrow, and this is a component you would normally use to label or tag content, or to print a simple line of plain text.

Exercise:

  1. Inside components, create a new folder called Eyebrow

  2. Inside the Eyebrow folder, create a new file called index.js

  3. In index.js, add the following code:

import React from 'react';

const Eyebrow = ({ children }) => {
  return (
    <div style={{ color: '#000' }}>
      {children}
    </div>
  );
};

export default Eyebrow;
  • First we import React to ensure we have all React-related resources available

  • Next we create a function in which we declared a variable named Eyebrow. Components are independent and reusable. They can either be Javascript functions or classes. Either way, they both return a piece of code that represents part of a web page.

  • The ({ children }) is a React built-in prop (short for “properties”), that can be used as a placeholder for any data you want to pass as the component's content.

  • The return method returns a description of what you want to see on the screen. React/Gatsby takes the description and displays the result. In particular, return returns a Gatsby element, which is a lightweight description of what to render.

  • Finally, export default Eyebrow makes this component available so it can be reused anywhere.

Let's use the new Eyebrow component (only as an example)

  1. In your editor, open components/Header/index.js

  2. At the top of the file, import the Eyebrow component: import Eyebrow from '../Eyebrow';

  3. After the closing of the Link tag (</Link>), add the Eyebrow component as follows:

<Eyebrow children='Learning Gatsby' />
  • After saving your changes if you visit your site's homepage you should see "Learning Gatsby" under the the Site's title.

  • In this example we have given the children prop a value of "Learning Gatsby". This is great because this means we can reuse the Eyebrow component anywhere and we can change its value to anything we want on each instance.

PreviousAbout ComponentsNextButton

Last updated 4 years ago

Was this helpful?