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

Was this helpful?

  1. Components

Heading

Building a Heading component that provides ways for changing the heading level (h1 - h6), as well as turn the heading into a link when needed, will make your component for flexible and reusable.

import React from 'react';
import PropTypes from 'prop-types';
import Link from '../Link';

import { childrenType } from '../../global/js/customPropTypes';
import styles from './styles.module.scss';

const Heading = ({
  headingLevel,
  url,
  children
}) => {
  const Element = `h${headingLevel}`;
  return (
    <Element className={styles.Heading}>
      {url ? (<Link to={url}>{children}</Link>) : children}
    </Element>
  );
};

export default Heading;

Heading.propTypes = {
  headingLevel: PropTypes.number,
  text: PropTypes.string,
  children: childrenType
};

Heading.defaultProps = {
  headingLevel: 4
};
PreviousButtonNextWorking with CSS

Last updated 4 years ago

Was this helpful?