gatsby

Cover Photo by Anas Alshanti on Unsplash

封面图片由Anas Alshanti拍摄于Unsplash

问题” (The “problem”)

When using the static site generator Gatsby you don’t have a base “App” component to play with. That said, there’s no component that wraps around your whole application where you can put your state that needs to be kept between routes/pages. Gatsby.js automatically (or automagically?) creates routes to pages you put in your page folder of your installation. Or, you create pages programmatically from your gatsby-node.js file.

使用静态网站生成器Gatsby时,您没有可使用的基本“应用”组件。 也就是说,没有组件可以包裹整个应用程序,您可以在其中放置需要保存在路由/页面之间的状态。 Gatsby.js自动(或自动地?)创建到放置在安装页面文件夹中的页面的路由。 或者,您可以通过gatsby-node.js文件以编程方式创建页面。

This will get us in trouble if we need, for example, a menu that should be visible and available for interaction on all our page routes. In my case, I had a mail form menu that could be shown or hidden in the right lower corner of my application. This component has a local state that will decide if the component is being shown or not. The below image shows the menu closed and opened.

例如,如果我们需要一个应该可见的菜单,并且可以在我们所有的页面路由上进行交互,那么这将给我们带来麻烦。 就我而言,我有一个邮件表单菜单,可以在应用程序的右下角显示或隐藏它。 该组件具有本地状态,该状态将决定是否显示该组件。 下图显示了关闭和打开的菜单。

So… this is our problem. How can we tackle it? There’s a number of ways to deal with this but one way, and the approach I took, is described below.

所以……这是我们的问题。 我们该如何解决? 有很多方法可以解决这个问题,但是下面将介绍一种方法以及我采用的方法。

解决方案 (The Solution)

I’ll go straight to the point. Gatsby has a file that’s named gatsby-browser.js. We can use this file to make components wrap around our complete App and pages!

我将直奔主题。 Gatsby的文件名为gatsby-browser.js。 我们可以使用此文件使组件环绕我们完整的应用程序和页面!

This is great!

这很棒!

This file lets us use the Gatsby Browser API. This API contains several useful functions but there’s one in particular that will fit our needs. It’s called wrapPageElement. Check out the code below. This is the actual code I used for my client’s app.

该文件使我们可以使用Gatsby Browser API。 该API包含几个有用的功能,但其中一个特别适合我们的需求。 它称为wrapPageElement。 查看下面的代码。 这是我用于客户应用程序的实际代码。

// gatsby-browser.js
// Import the component at the top of the file
import MailWidgetWrapper from './src/components/MailWidgetWrapper';export const wrapPageElement = ({ element, props }) => (<MailWidgetWrapper {...props}>{element}</MailWidgetWrapper>
);

Here, I’ve created a wrapper component that will be available on all the routes and pages in Gatsby. That’s Awesome! And just what we need. The wrapper component looks like this:

在这里,我创建了一个包装器组件,该组件将在盖茨比的所有路线和页面上使用。 棒极了! 而正是我们需要的。 包装器组件如下所示:

// MailWidgetWrapper.js
import React from 'react';import MailWidget from './MailWidget';const MailWidgetWrapper = ({ children }) => (<>{children}<MailWidget /></>
);export default MailWidgetWrapper;

This is a really simple React Component who’s only function is to wrap our app and provide it with the MailWidget component. But how does wrapPageElement work?

这是一个非常简单的React Component,它的唯一功能是包装我们的应用程序并为其提供MailWidget组件。 但是wrapPageElement如何工作?

wrapPageElement (wrapPageElement)

First, I also highly recommend using gatsbyjs.org as much as you can for finding answers to anything regarding Gatsby. The site is excellent and full of really good and thorough explanations of most problems you will encounter.

首先,我也强烈建议您尽可能多地使用gatsbyjs.org,以找到有关盖茨比的所有问题的答案。 该站点非常出色,并为您将遇到的大多数问题提供了非常好的详尽的解释。

In our case, if you look at the code above, we have two parameters that get created for us in the wrapPageElement callback function: element and props.

在我们的例子中,如果您看上面的代码,则在wrapPageElement回调函数中为我们创建了两个参数: element和props。

You should be familiar with props if you use React so they need no further introduction. In this case, the props are used by the page we’re currently on. We don’t need to use any of these props, as we only need to use the children (automatically created by React) prop.

如果您使用React,那么您应该熟悉道具,因此它们不需要进一步介绍。 在这种情况下,道具将被当前页面所使用。 我们不需要使用任何这些道具,因为我们只需要使用子元素(由React自动创建)道具即可。

The MailWidgetWrapper just renders the children and the MailWidget. The children are the page we’re sending into the MailWidgetWrapper component from the gatsby-browser.js file, as shown below. The actual page lives in the element parameter and that’s the one we’re sending in with the expression {element}.

MailWidgetWrapper仅呈现子项和MailWidget 。 子级是我们从gatsby-browser.js文件发送到MailWidgetWrapper组件的页面 ,如下所示。 实际页面位于element参数中,这就是我们要使用表达式{element}

<MailWidgetWrapper {…props}>{element}</MailWidgetWrapper>

So in short, the parameters we get from wrapPageElement can be summarized:

简而言之,我们可以总结从wrapPageElement获得的参数:

The props parameter are the props from the actual page we’re on. And the element parameter is the actual page we’re on

props参数是来自我们实际页面上的props。 而element参数是我们所在的实际页面

MailWidget组件 (The MailWidget Component)

My actual MailWidget component is quite large and has a lot of code that’s not relevant here. That’s why I'm just showing you a simple scaffolded example version of a MailWidget component below. This component is not actually relevant for the task of explaining the wrapPageElement function.

我实际的MailWidget组件很大,并且有很多与此处无关的代码。 这就是为什么我MailWidget您展示MailWidget组件的简单脚手架示例版本。 该组件实际上与解释wrapPageElement函数的任务wrapPageElement

The component can virtually be anything you like and has nothing to do with the implementation above. In my case it’s a MailWidget. It’s all up to you and what stateful component/s you need to be available on all your page routes.

该组件实际上可以是任何您喜欢的组件,并且与上述实现无关。 就我而言,这是一个MailWidget 。 这完全取决于您,以及您需要在所有页面路由中使用哪些状态组件。

// MailWidget.js
import React, { useState } from 'react';const MailWidget = () => {const [isVisible, setIsVisible] = useState(false);const toggleVisible = () => {setIsVisible(!isVisible);};return (<div className={isVisible ? 'visible' : ''}><button type="button" onClick={toggleVisible}>Hide/Show MailWidget</button><h1>Hello, I'm your mailwidget</h1></div>);
};
export default MailWidget;

By the way, I’m all in on Hooks. I love Hooks and will use them in everything I do in React! That’s why I created my state with the useState hook in this one. The component above just uses a local state to decide if it should show itself or not.

顺便说一句,我全力以赴。 我喜欢Hook,并将在我在React中所做的一切中使用它们! 这就是为什么我在此代码中使用useState钩子创建状态的原因。 上面的组件仅使用本地状态来决定是否应显示自己。

结论 (Conclusion)

There you have it! Hopefully, you’ve learned that it’s not difficult to have a component keeping its state between pages in Gatsby. And we all love Gatsby.js don’t we?

gatsby_如何在Gatsby.js中使用本地状态保持页面之间的状态相关推荐

  1. 如何在Node.js中获取本机本地IP地址

    最近在做Cloud related的项目时,遇到一个问题,就是如何在Node.js中获取本机的IP地址.Node.js提供的API中,只能获取本机的hostname. os = require('os ...

  2. 如何在css文件中使用本地ttf/woff/woff2字体?

    如何在css文件中使用本地ttf/woff/woff2字体? 1.首先下载ttf.woff.woff2字体文件 免费的字体文件可以上阿里矢量图库进行下载,不过数量很少. 2.在css文件中配置相应代码 ...

  3. 如何在Tensorflow.js中处理MNIST图像数据

    by Kevin Scott 凯文·斯科特(Kevin Scott) 如何在Tensorflow.js中处理MNIST图像数据 (How to deal with MNIST image data i ...

  4. 如何在node.js中发出HTTP POST请求?

    如何在node.js中使用数据发出出站HTTP POST请求? #1楼 如果您使用请求库,这会变得更容易. var request = require('request');request.post( ...

  5. JS中调用本地Winform程序并传递参数

    场景 JS中调用本地exe程序: JS中调用本地exe程序_BADAO_LIUMANG_QIZHI的博客-CSDN博客 在上面的基础上怎样在js中调用本地winform程序并且传递参数. 注: 博客: ...

  6. vue.js中mock本地json数据

    vue.js中mock本地json数据 新版本的vue项目中已经将dev-server.js,dev-client.js两个js文件合并到了webpack.dev.conf.js文件中,以下分别是新旧 ...

  7. 如何在Node.js中打印堆栈跟踪?

    本文翻译自:How to print a stack trace in Node.js? 有谁知道如何在Node.js中打印堆栈跟踪? #1楼 参考:https://stackoom.com/ques ...

  8. gatsby_如何使用Gatsby.js来获取内容

    gatsby by Dimitri Ivashchuk 由Dimitri Ivashchuk 如何使用Gatsby.js来获取内容 (How to source content with Gatsby ...

  9. react本地储存_如何在React项目中利用本地存储

    react本地储存 以及为什么要这么做. 本地存储是现代Web浏览器固有的Web API. 它允许网站/应用程序在浏览器中存储数据(简单和有限),从而使数据在以后的浏览器会话中可用. 在开始学习本教程 ...

最新文章

  1. OpenCV 笔记(03)— 读取视频、通过摄像头采集视频、采集视频 canny 边缘检测
  2. 《java第二次实验》
  3. Openstack-L 路由注入方式
  4. range python 3.6 type class_Python 3.6 有什么新特性
  5. 一篇文章搞懂腾讯云AI平台的人工智能IDE:TI-ONE
  6. 栏目图片 栏目描述_昕街拍|长期福利栏目来啦,秀街拍赢礼品!
  7. 推荐系统--矩阵分解(3)
  8. ruby array_Ruby中带有示例的Array.fill()方法(3)
  9. 【Win 10 应用开发】分析 URI 中的查询字符串
  10. php生成字母数字订单,php生成唯一订单号可控制位数字母和数字
  11. mavon-editor文本编辑器初体验(一)
  12. I00034 累加与累乘
  13. 计算机光驱里有硬盘,笔记本电脑光驱位装机械硬盘有以下危害
  14. Tilt Five AR桌游体验:概念很新颖,但缺乏高质量内容?
  15. windows快速生成ssh key
  16. 干货 | 深度理解数据采集与埋点,提高自主数据分析能力!
  17. mysql的prepared_statement
  18. CRF进行实体的识别
  19. 打开网页报错:net::ERR_CONTENT_LENGTH_MISMATCH,网页打开时显示不全,需要刷新几次才行
  20. 记第一次参加五一数学建模竞赛

热门文章

  1. 【mAP】关于目标检测mAP的一些理解
  2. 【python】 合并列表的方法
  3. 04 理解SQL与T-SQL的概念测试分析 1214
  4. requests-使用代理proxies
  5. mysql进阶,03-事务2-多个客户端一起操作时的情况
  6. Symfony 框架实战教程——第一天:创建项目(转)
  7. sql右下角图标工具
  8. Macbook pro wifi连接无线路由不稳定掉线的解决办法
  9. 一个flash网页图片播放器
  10. Centos7 安装Docker(v2021 version 20.10.5) 并通过docker-compose运行TiDB集群