简体   繁体   English

在 JSX 中渲染组件与通过函数渲染

[英]Rendering a component in JSX vs via function

When using React, what is the difference (and when should each method be applied) when rendering a component?使用 React 时,渲染组件时有什么区别(以及何时应该应用每种方法)?

Does this impact the component lifecycle in any way?这是否会以任何方式影响组件生命周期? Or impact how hooks are run in the component?或者影响钩子在组件中的运行方式?

Method 1:方法一:

class App extends React.Component {
  ...
  function getComponent() {
    return <Component />
  }
  render() {
   return this.getComponent()
  }
}

Method 2:方法二:

class App extends React.Component {
  ...
  render() {
   return <Component />
  }
}

( Note: The OP has now changed the question, it used to have return {this.getComponent()} in Method 1.) 注意: OP 现在已经改变了问题,它曾经在方法 1 中return {this.getComponent()} 。)

render in Method 1 is incorrect (well, it was before the edit) , it should be:方法 1 中的render不正确(嗯,是在编辑之前) ,应该是:

render() {
  return this.getComponent() // No {} wrapper
}

You need the {} within a JSX context, but you're not in a JSX context there.您需要在 JSX 上下文中使用{} ,但您不在那里的 JSX 上下文中。 For instance, if you wanted to wrap what this.getComponent returned in a div , you'd use the JSX expression to define the div 's children within the JSX defining the div :例如,如果您想将this.getComponent返回的内容包装在div ,您可以使用 JSX 表达式在定义div的 JSX 中定义div

render() {
  return <div>{this.getComponent()}</div>
}

With the {} sorted out, whether you use Method 1 or Method 2 is up to you.整理好{}后,您使用方法 1 还是方法 2 由您决定。 If you have substantial parts of the render that you want to move into their own functions, that's fine.如果您想将渲染的重要部分移到它们自己的功能中,那很好。 For instance:例如:

render() {
    return (
        <div>
            {this.getHeader()}
            {this.getBody()}
            {this.getFooter()}
        </div>
    );
}

...although I think I'd probably argue at that point that without a good counter-argument, the header, body, and footer should probably be components (perhaps function components). ...虽然我想我可能会在这一点上争辩说,如果没有一个好的反驳,页眉、正文和页脚可能应该是组件(也许是函数组件)。 But the occasional helper function call like that is fine.但是偶尔这样的辅助函数调用是没问题的。

Does this impact the component lifecycle in anyway?这是否会影响组件生命周期?

No. It's just a function call within render .不,它只是render一个函数调用。

There is no real difference between both.两者之间没有真正的区别。 I'd personally use only one render() method as much as possible, then when the method gets too big, extract parts of it into their own method.我个人尽可能只使用一种render()方法,然后当方法变得太大时,将其部分提取到自己的方法中。

I have found this great article by Kent C. Dodds.我找到了 Kent C. Dodds 的这篇很棒的文章 An extract of the article is:文章摘录如下:

React doesn't know the difference between us calling a function in our JSX and inlining it. React 不知道我们在 JSX 中调用函数和内联它之间的区别。 So it cannot associate anything to the Counter function, because it's not being rendered like a component.所以它不能将任何东西关联到Counter函数,因为它不像组件那样被渲染。

This is why you need to use JSX (or React.createElement) when rendering components rather than simply calling the function.这就是为什么在渲染组件时需要使用 JSX(或 React.createElement)而不是简单地调用函数的原因。 That way, any hooks that are used can be registered with the instance of the component that React creates.这样,任何使用的钩子都可以注册到 React 创建的组件实例。

With this in mind, it sounds like it's better to use JSX when rendering a component that uses hooks.考虑到这一点,听起来似乎在渲染使用钩子的组件时使用 JSX 更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM