简体   繁体   English

在 React 循环中使用组件函数

[英]Use component function in React loop

I heard that () is not good at the rendering performance so it should not be used.听说 () 的渲染性能不好,所以不应该使用。 However, in this case, it can not get the results when passing the function without ().但是,在这种情况下,在不带()的情况下传递函数时是无法得到结果的。

You can check the reseult in CodeSendbox: Link您可以在 CodeSendbox 中查看结果: 链接

const comp = () => {
  return <div>This is Component</div>;
};

const elements = [comp(), comp];  // Please Attention This Line!
const items = [];

for (const [index, value] of elements.entries()) {
  items.push(
    <div>
      {index}: {value} <br />
    </div>
  );
}

export default function Index() {
  return <>{items}</>;
}

Result is:结果是:

0:
This is Component

1: 

Why only comp() but not comp ?为什么只有comp()而不是comp What is the best practice in this case?在这种情况下,最佳做法是什么?

comp is an arrow function that returns a JSX literal. comp是一个返回 JSX 文字的箭头函数。 It's a "function" that is stored in a variable called comp .它是一个存储在名为comp的变量中的“函数”。 When called/invoked as comp() , notice the parenthesis, it is executed and the computed return value is returned in the array.当作为comp()调用/调用时,注意括号,它被执行并且计算的返回值在数组中返回。 When used without the parenthesis, like comp , this is simply the reference to the variable (that can be invoked like a function).当不带括号使用时,如comp ,这只是对变量的引用(可以像函数一样调用)。

Functions aren't valid react children nor valid JSX.函数不是有效的反应孩子,也不是有效的 JSX。

Introducing JSX介绍 JSX

const comp = () => {
  return <div>This is Component</div>;
};

const elements = [comp(), comp];  // saves a JSX literal, and a function
const items = [];

for (const [index, value] of elements.entries()) {
  items.push(
    <div>
      {index}: {value} <br /> // trying to render a function here in `value`
    </div>
  );
}

export default function Index() {
  return <>{items}</>;
}

I assume you got the idea () is "not good at rendering performance" from some documentation about in-line anonymous arrow functions and event handlers and mapping an array of data.我假设您从一些关于内嵌匿名箭头函数和事件处理程序以及映射数据数组的文档中了解到() “不擅长渲染性能”。 That is a different issue altogether.那是完全不同的问题。 I believe your situation is just a misunderstanding about how arrow functions are invoked.我相信您的情况只是对如何调用箭头函数的误解。

You can use JSX instead of calling the function:您可以使用 JSX 而不是调用函数:

const comp = () => {
  return <div>This is Component</div>;
};

const elements = [comp, comp];  // Remove the function call, save only the reference
const items = [];

// need to use Value with V instead of v for JSX
for (const [index, Value] of elements.entries()) {
  items.push(
    <div>
      {index}: <Value/> <br /> // render as JSX
    </div>
  );
}

export default function Index() {
  return <>{items}</>;
}

Here's a sandbox link showing how it works: https://codesandbox.io/s/cranky-mclean-7ymzr这是一个沙箱链接,显示了它是如何工作的: https : //codesandbox.io/s/cranky-mclean-7ymzr

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

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