[英]React: jsx in a variable vs a function vs a separate component
For rendering smaller components/jsx within a bigger component, there are multiple approaches that one can follow.为了在更大的组件中渲染更小的组件/jsx,可以采用多种方法。 For example, consider this:
例如,考虑一下:
Method 1:方法一:
function BigComponent(props) {
const renderSmallComponent1 = () => <div>{props.a}</div>;
const renderSmallComponent2 = () => <div>{props.b}</div>;
return (
<div>
{renderSmallComponent1()}
{renderSmallComponent2()}
</div>
)
}
Method 2:方法二:
function BigComponent(props) {
const smallComponent1 = <div>{props.a}</div>;
const smallComponent2 = <div>{props.b}</div>;
return (
<div>
{smallComponent1}
{smallComponent2}
</div>
)
}
Method 3:方法三:
function SmallComponent1({ a }) {
return <div>{a}</div>;
}
function SmallComponent2({ b }) {
return <div>{b}</div>;
}
function BigComponent(props) {
return (
<div>
<SmallComponent1 a={props.a} />
<SmallComponent2 b={props.b} />
</div>
)
}
I am just trying to understand the difference in these 3 in terms of我只是想了解这三个方面的区别
These are the things that I understand:这些是我理解的事情:
SmallComponent
are React components which are rendered in another component, so they would have a component lifecycle, while in method 1 and 2, they are simple jsx, which does not have lifecycle, so they would not be mounted / unmounted as React componentsSmallComponent
都是 React 组件,在另一个组件中渲染,所以它们会有一个组件生命周期,而在方法一和方法二中,它们是简单的 jsx,没有生命周期,所以它们不会被挂载/卸载作为 React 组件 A few other helpful articles:其他一些有用的文章:
UPDATE : it seems observation 1 is incorrect as all 3 of them would still be rendered as react components, and hence would have a component lifecycle.更新:似乎观察 1 是不正确的,因为它们中的所有 3 个仍将呈现为反应组件,因此将具有组件生命周期。 So react would mount/unmount them.
所以反应会挂载/卸载它们。
UPDATE 2 : No, observation 1 is correct, method 1 and 2 are both treated as regular jsx as part of the BigComponent and they are not treated as react component which have a lifecycle.更新 2 :不,观察 1 是正确的,方法 1 和 2 都被视为常规 jsx 作为 BigComponent 的一部分,并且它们不被视为具有生命周期的反应组件。
UPDATE 3 : There is another method Method 4:更新 3 :还有另一种方法方法 4:
function BigComponent(props) {
const SmallComponent1 = () => {
return <div>{props.a}</div>;
}
const SmallComponent2 = () => {
return <div>{props.b}</div>;
}
return (
<div>
<SmallComponent1 />
<SmallComponent2 />
</div>
)
}
this is similar to Method 3, but Method 3 vs Method 4 is slightly different in execution, when debugging through dev tools.这与方法 3 类似,但是通过开发工具进行调试时,方法 3 与方法 4 在执行上略有不同。
Method 2:方法二:
function BigComponent(props) {
const smallComponent1 = <div>{props.a}</div>;
const smallComponent2 = <div>{props.b}</div>;
return (
<div>
{smallComponent1}
{smallComponent2}
</div>
)
}
useMemo()
if the result of those smallComponents are coming from an expensive computation.useMemo()
包装它们。 Method 3:方法三:
function SmallComponent1({ a }) {
return <div>{a}</div>;
}
function SmallComponent2({ b }) {
return <div>{b}</div>;
}
function BigComponent(props) {
return (
<div>
<SmallComponent1 a={props.a} />
<SmallComponent2 b={props.b} />
</div>
)
}
React needs to resolve reference as well as execute the function after resolving the reference. React 需要解析引用并在解析引用后执行 function。
It is a composition of react's actual child components into a large Component.它是将 React 的实际子组件组合成一个大组件。
Child components are allowed to have their own hooks
.允许子组件有自己的
hooks
。
Child components are not re-initialized but are re-rendered if BigComponent state is changed.如果更改 BigComponent state,子组件不会重新初始化,但会重新呈现。
There is chance of SmallComponent1 and SmallComponent2 getting re-rendered multiple times on BigComponents rendering once if small components are updating thier own state based on props change in parents.如果小组件根据父项中的道具更改更新它们自己的 state,则 SmallComponent1 和 SmallComponent2 有可能在 BigComponents 渲染一次时被重新渲染多次。
if each SmallComponents are supposed to use multiple props which state of BigComponents, Keeping SmallComponents outside BigComponent does offer good developer experience.如果每个 SmallComponents 都应该使用 state BigComponents 的多个 props,将 SmallComponents 保留在 BigComponent 之外确实提供了良好的开发人员体验。
I hope Method 1 and Method 4 can also be understood using these above points.我希望方法1和方法4也可以通过以上几点来理解。
Note: childcomponents stored in variable and childcompoents as function becomes tricker if your application logic is using ref or DOM element for maininting focus or anchor point of rendering.注意:如果您的应用程序逻辑使用 ref 或 DOM 元素来维护焦点或渲染锚点,则存储在变量和子组件中的子组件 function 会变得更加棘手。
Have you taken a look at the compiled JS in a React project?你看过 React 项目中编译好的 JS 吗?
JSX tags are essentially transformed in to React.createElement
statements. JSX 标签本质上被转换为
React.createElement
语句。 You can read the docs here .您可以在此处阅读文档。 Essentially the syntax is:
本质上,语法是:
React.createElement(FunctionOrClassComponent, { props }, ...children)
In all three of your examples this would take place.在您的所有三个示例中,都会发生这种情况。 In all three examples, the smaller components are functional components rather than class components.
在所有三个示例中,较小的组件都是功能组件,而不是 class 组件。 That is to say, they don't have the React lifecycle methods of a class component, but they can use equivalent React hooks - should you want to.
也就是说,它们没有 class 组件的 React 生命周期方法,但是如果您愿意,它们可以使用等效的 React 钩子。
Edited: Evaluation (instantiation and rendering) depends on your render logic.已编辑:评估(实例化和渲染)取决于您的渲染逻辑。 If you have conditional rendering statements or your functions return null (or less content) based on certain conditions, then obviously you're doing less work.
如果您有条件渲染语句或您的函数基于某些条件返回 null(或更少的内容),那么显然您所做的工作更少。 And as you rightly pointed out in the comments below, when you assign a JSX.Element to a variable, that is evaluated inline rather than as a result of a function - so that happens immediately.
正如您在下面的评论中正确指出的那样,当您将 JSX.Element 分配给变量时,该变量是内联评估的,而不是作为 function 的结果 - 所以这会立即发生。
To me, all three are valid approaches.对我来说,这三种方法都是有效的。 To address your questions:
为了解决您的问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.