首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GatsbyJS/ReactJS扩展React.Component的const Page与类Page的不同

使用GatsbyJS/ReactJS扩展React.Component的const Page与类Page的不同
EN

Stack Overflow用户
提问于 2020-11-17 00:24:09
回答 2查看 411关注 0票数 2

我最近开始使用盖茨比( Gatsby ),在理解其中的一些概念(实际上可能来自于React)方面遇到了一些困难。

例如:我正在为I18n使用一个名为gatsby插件-intl的插件,我很难理解如何将文档中的代码放入代码中。

我从初学者包中获得的代码包含了src/pages/index.js中这样的代码文件

代码语言:javascript
复制
import React from "react";

class RootIndex extends React.Component {
  render() {
    return (
...

但是,我经常会遇到这样的代码示例:

代码语言:javascript
复制
import React from "react"

const RootIndex = () => {
  return (
...

显然,这两种方法产生的输出是相同的。但是,当我遇到一个教程告诉我要实现这样的代码时:

代码语言:javascript
复制
import React from "react"
import { injectIntl, Link, FormattedMessage } from "gatsby-plugin-intl"

const RootIndex = ({ intl }) => {
  return (
    <Layout>
      <SEO
        title={intl.formatMessage({ id: "title" })}
      />
...

我不知道如何使用定义类的第一种方法将参数intl放在哪里。这不管用

代码语言:javascript
复制
import React from "react";
import { injectIntl, Link, FormattedMessage } from "gatsby-plugin-intl"

class RootIndex extends React.Component {
  render({ intl }) {
    return (
      <Layout>
        <SEO
          title={intl.formatMessage({ id: "title" })}
        />
...

我希望我的描述和问题是清楚的。在这里,我缺少更简洁的知识。哪条路是正确的?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-17 06:10:53

它们在产出方面完全相同,但在性能和性能方面却不一样。它们被称为有状态组件(基于类的组件)和无状态组件(功能组件)。

通常,如果您只想呈现一个组件,我建议使用一个功能组件,因为它具有更好的性能,并使代码更清晰、更可重用,从而使您能够更好地细化功能。

另一方面,如果您使用的是一些编程模式 (BLoCMVVM等),您可能需要创建类来保存业务逻辑,这样就可以相互扩展。

基于类的组件(状态)

  • 基于类的组件利用ES6类并在React中扩展组件类.
  • 有时被称为“智能”或“有状态”组件,因为它们倾向于实现逻辑和状态。
  • React生命周期方法可以在类组件(componentDidUpdatecomponentDidMount等)中使用。
  • 您将道具向下传递到类组件,并使用this.props (this关键字)访问它们。
  • 您需要绑定/取消绑定事件和函数。

职能部分(无国籍)

  • 功能组件是基本的JavaScript函数。这些通常是箭头函数,但也可以使用常规的function关键字创建。
  • 与基于类的组件相反,这些组件有时被称为“哑”或“无状态”组件,因为它们只是接受数据并以某种形式显示它们;也就是说,它们主要负责呈现UI。
  • React生命周期方法(例如,componentDidMount)不能在功能组件中使用。但是,它们允许您使用React (useStateuseEffect等)来处理所有典型的生命周期好处--添加更干净和可重用的代码。
  • 函数组件中没有使用呈现方法,函数的返回将处理它。因此,他们主要负责UI。
  • 功能组件可以接受和使用道具。
  • 他们的表现略好于基于班级的。但在我看来,最重要的是它们更干净,更可重用。

参考文献:

关于您的实现,由于gatsby-plugin-intl使用内部挂钩(useIntl),下面的代码应该可以工作:

代码语言:javascript
复制
import React from "react"
import { useIntl, Link, FormattedMessage } from "gatsby-plugin-intl"

const RootIndex = (props) => { 
 const intl = useIntl();
  return (
    <Layout>
      <SEO
        title={intl.formatMessage({ id: "title" })}
      />
...

您缺少了允许代码使用const intl = useIntl();国际化的gatsby-plugin-intl。而不是通过props传递它,一旦您导入它,您需要在return语句之上执行它。

票数 2
EN

Stack Overflow用户

发布于 2022-02-23 10:45:19

同样的问题,尝试在locationcrumbLabel变量中添加Layout.js

添加

代码语言:javascript
复制
this.state = {
        location: props.location,
        crumbLabel: props.crumbLabel,
    };

代码语言:javascript
复制
const { location } = this.state;
const { crumbLabel } = this.state;

对我来说很有用。

示例代码

src/pages/index.js

代码语言:javascript
复制
import React from 'react';
import Layout from '../components/Layout';

const IndexPage = ({location}) => (
  <Layout location={location} crumbLabel="Home">
    ...
  </Layout>
);

export default IndexPage;

src/components/Layout.js

代码语言:javascript
复制
import React, { Component } from 'react';
import { Breadcrumb } from 'gatsby-plugin-breadcrumb'; 

class Layout extends Component {
 constructor(props) {
    super(props);
    this.state = {
        location: props.location,
        crumbLabel: props.crumbLabel,
    };
  }

  render() {
    const { location } = this.state;
    const { crumbLabel } = this.state;
    return (
  
      <Breadcrumb location={location} crumbLabel={crumbLabel} /> 
     
    );
  }
}
export default Layout;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64867636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档