Skip to content

Instantly share code, notes, and snippets.

@hdknr
Last active September 5, 2024 04:23
Show Gist options
  • Save hdknr/c11a71f5f88d3d0f6abb643fb5a8e0a6 to your computer and use it in GitHub Desktop.
Save hdknr/c11a71f5f88d3d0f6abb643fb5a8e0a6 to your computer and use it in GitHub Desktop.
Gatsby レイアウト

Gastby レイアウト

wrapper.js

Gatsbyのwrapper.jsファイルは、ページ全体をラップするために使用されるファイルです。 このファイルを使用することで、特定のHTML要素やReactプロバイダーでページをラップすることができます。 これにより、ページ全体に共通のレイアウトや機能を簡単に適用できます。

例えば、以下のようにwrapper.jsファイルを作成し、ページを特定のコンポーネントでラップすることができます。

import React from 'react';
import Layout from './src/components/Layout';

export const wrapPageElement = ({ element, props }) => {
  return <Layout {...props}>{element}</Layout>;
};

この例では、Layoutコンポーネントでページ全体をラップしています。これにより、すべてのページに共通のレイアウトが適用されます。

さらに詳しい情報や具体的な使用例については、Gatsbyの公式ドキュメントを参考にすると良いでしょう¹。

他に知りたいことがあれば教えてくださいね!

ソース: Copilot との会話、 2024/9/4 (1) gatsby-plugin-wrap-pages | Gatsby. https://www.gatsbyjs.com/plugins/gatsby-plugin-wrap-pages/. (2) Gatsby公式チュートリアルやったよ #JavaScript - Qiita. https://qiita.com/irico/items/cf87eb29ecaf7e135fcd. (3) Gatsby.js 入門 - Zenn. https://zenn.dev/dev8/articles/gatsby_tutorial. (5) en.wikipedia.org. https://en.wikipedia.org/wiki/Gatsby_(software).

wrapper.js:

import React from "react";
import Layout from "./layout";
export const wrapPageElement = ({ element }) => {
  return (
    <>
      <Layout>{element}</Layout>
    </>
  );
};

layout.js:

import PropTypes from "prop-types";
import React from "react";
  
const Layout = ({ children }) => {
    return <>{children}</>;
};
  
Layout.propTypes = {
    children: PropTypes.node.isRequired,
};
  
export default Layout;

wrapPageElement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment