盖茨比乔布斯_使用盖茨比的useStaticQuery挂钩的快速指南

news/2024/7/5 7:15:23

盖茨比乔布斯

The useStaticQuery React Hook was added to Gatsby.js starting with version 2.1.0, and it’s an incredibly convenient way to make (build-time) GraphQL queries from any component within a Gatsby site. In this quick lesson, we’ll go over how to implement useStaticQuery in your project!

2.1.0版开始, useStaticQuery React Hook已添加到Gatsby.js中 ,这是从Gatsby站点内的任何组件进行(生成时)GraphQL查询的一种非常方便的方法。 在本快速课程中,我们将介绍如何在您的项目中实现useStaticQuery

概述和要求 (Overview and Requirements)

For this simple example, we’re going to add a ContactInfo component to our website. This will just be a re-usable component that fetches and displays some contact info from your Gatsby site’s siteMetadata configuration. Let’s get started!

对于这个简单的示例,我们将向我们的网站添加一个ContactInfo组件。 这只是一个可重用的组件,可从您的Gatsby网站的siteMetadata配置中获取并显示一些联系信息。 让我们开始吧!

This lesson only has one requirement: that you already have a Gatsby project set up, and you’re ready to edit along with me! If you need help getting to that point, just follow along with the intro tutorial in the Your First Steps with Gatsby v2 post, and then come back here.

本课仅有一个要求:您已经设置了一个Gatsby项目,并且可以和我一起编辑了! 如果您需要帮助,请按照“使用Gatsby的第一步” v2帖子中的入门教程进行操作,然后返回此处。

添加联系信息 (Add Contact Info)

To begin, let’s add some extra contact info to the siteMetadata object inside the gatsby-config.js file. To keep it simple, we will just add a phone number and an email address:

首先,让我们添加一些额外的联系信息向siteMetadata里面物体gatsby-config.js文件。 为简单起见,我们只添加一个电话号码和一个电子邮件地址:

gatsby-config.js
gatsby-config.js
siteMetadata: {
  title: "Gator's Empanada Express",
  siteUrl: "https://example-site.com/",
  phone: "(555) 567-0123",
  email: "info@example-site.com",
}
// plugins: { ... }

创建组件 (Create the Component)

With our contact data in place, we just need to make a component that fetches and displays it. And of course, we’ll make use of the useStaticQuery Hook to do it!

有了我们的联系数据,我们只需要制作一个获取并显示它的组件。 当然,我们将利用useStaticQuery Hook做到这一点!

Let’s create a new file at /components/ContactInfo.js, and then add the following code:

让我们在/components/ContactInfo.js处创建一个新文件,然后添加以下代码:

/components/ContactInfo.js
/components/ContactInfo.js
import React from "react";
import { useStaticQuery, graphql } from "gatsby";

const ContactInfo = () => {
  const data = useStaticQuery(graphql`
    query ContactInfoQuery {
      site {
        siteMetadata {
          phone,
          email
        }
      }
    }
  `);

  const { phone, email } = data.site.siteMetadata;

  return (
    <div>
      <h3>Contact Us:</h3>
      <p>phone: {phone}</p>
      <p>
        email: <a href={`mailto:${email}`}>{email}</a>
      </p>
    </div>
  )
};

export default ContactInfo;

See how simple this is? No confusing render props to deal with, and we’re making a GraphQL query within a non-page component! 🎉

看看这有多简单? 没有令人困惑的渲染道具要处理,我们正在非页面组件内进行GraphQL查询 🎉

You can now include this component anywhere on your site to display the contact info, and anytime you change these settings in siteMetadata they will update site-wide.

现在,您可以在网站上的任何位置包括此组件以显示联系信息,并且只要您在siteMetadata更改这些设置,它们就会在siteMetadata网站范围内更新。

Note that this data does not have to come from siteMetaData! You could fetch data from any source where you would make a GraphQL query, such as fetching a list of recent blog posts, customer ratings, or upcoming product releases.

请注意,此数据不必来自siteMetaData ! 您可以从进行GraphQL查询的任何来源获取数据,例如获取最近博客帖子,客户评分或即将发布的产品列表。

例外情况 (Exceptions)

While useStaticQuery is incredibly useful, it currently has two limitations:

尽管useStaticQuery非常有用,但目前有两个限制:

  • When using useStaticQuery, you cannot use variables within GraphQL queries. (It’s called useStaticQuery for a reason, after all! 😁) You still have to do those via page-level queries.

    使用useStaticQuery ,不能在GraphQL查询中使用变量。 ( useStaticQuery有一个原因,它称为useStaticQuery !😁)您仍然必须通过页面级查询来执行这些操作。

  • You can only use one instance of useStaticQuery per component. (But you can make several queries inside it! You could query siteMetadata, recent posts, and more all via one query/instance.)

    每个组件只能使用一个useStaticQuery实例。 (但是您可以在其中进行几个查询!您可以通过一个查询/实例查询siteMetadata,最新帖子等)。

结论 (Conclusion)

As you can see, Gatsby’s built-in useStaticQuery Hook is an extremely useful and easy-to-use feature to incorporate into your Gatsby Toolbelt! I use it quite frequently, particularly for things like recent blog posts or product releases. (And yes, I have even used it for meta/contact info! 👍)

如您所见,Gatsby的内置useStaticQuery Hook是一项非常有用且易于使用的功能,可以整合到您的Gatsby Toolbelt中 ! 我经常使用它,尤其是在最近的博客文章或产品发布中。 (是的,我什至将它用于元数据/联系信息!👍)

For more detailed info and examples, I recommend heading over to Gatsby’s official documentation on useStaticQuery. Like always, their docs are well-written and heavily maintained, so that page will always provide the most up-to-date information and examples!

有关更多详细信息和示例,我建议转到Gatsby的useStaticQuery官方文档 。 像往常一样,他们的文档写得很好并且维护得很严格,因此该页面将始终提供最新的信息和示例!

翻译自: https://www.digitalocean.com/community/tutorials/gatsbyjs-usestaticquery-hook-in-gatsby

盖茨比乔布斯


http://www.niftyadmin.cn/n/3649311.html

相关文章

客户端封装Fragment和Activity

一、封装activity public abstract class Activity extends AppCompatActivity {Overrideprotected void onCreate(Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);initWindows();if (initargs(getIntent().getExtras())) {// 得到界面Id并设置到Ac…

Android Studio中的手机通讯录开发

Android Studio中的手机通讯录&#xff0c;包含功能&#xff08;按首字母排序&#xff0c;动态添加&#xff09; 第一次写博客&#xff0c;也刚踏入工作&#xff0c;想着把自己在项目中遇到的问题&#xff0c;以及自己在工作中所做的项目记录下来&#xff0c;方便以后自己查找…

redis排序_如何在Redis中管理排序集

redis排序介绍 (Introduction) Redis is an open-source, in-memory key-value data store. In Redis, sorted sets are a data type similar to sets in that both are non repeating groups of strings. The difference is that each member of a sorted set is associated w…

Android版本和API Level的对应关系

在开发Android时&#xff0c;老是不知道Android版本号和对应API level&#xff0c;这个问题真是麻烦&#xff0c;我们在发布声波传输SDK时也遇到这样的问题&#xff0c;版本号是对外发布的版本号&#xff0c;一般都是主版本号.子版本号.修正版本号的命名规则&#xff0c;说白了…

[dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书

使用HttpWebRequest请求远端服务器时如何加载SSL证书编写者&#xff1a;郑昀UltraPower首先加上引用“System.Security.DLL”&#xff0c;其次在工程中using System.Security.Cryptography.X509Certificates;这样就可以使用“X509Certificate Class”了&#xff0c;它的定义参见…

JDBC的使用(一)

Java 中的数据存储技术 在Java中&#xff0c;数据库存取技术可分为如下几类&#xff1a; ①JDBC直接访问数据库 ②JDO技术 ③第三方O/R工具&#xff0c;如Hibernate, ibatis 等 JDBC是java访问数据库的基石&#xff0c;JDO, Hibernate等…

若依项目(前后端分离)

最近在基于若依项目二次开发中&#xff0c;遇到表设计问题&#xff0c;然后看了若依想项目前后端分离视频中&#xff0c;也是不太懂表的设计问题

Android常用Adapter代码例子

ArrayAdapter   总是感觉写自己的博客才更能够学到东西&#xff0c;网上尽管有很多好的资料&#xff0c;但是参差不齐&#xff0c;需要浪费大量时间才能够找到最需要的&#xff0c;索性写自己最需要的东西。 Adapter是适配器的意思&#xff0c;在Android中大量的使用到了List…