简体   繁体   English

由另一个 function 组件渲染一个 JSX 部件 - reactJS

[英]render a part JSX by another function component - reactJS

I have a function component like that我有一个像这样的 function 组件

function myMainComponent() {

const MyUIA() {
 //some calulations about attrs
 return (<div><ComponentA {...attrs}></div>)
}

const MyUIB() {
 //some calulations about attrs
 return (<div><ComponentA {...attrs}></div>)
}

// Way A
/*
return (
<div>
  <MyUIA/>
  <MyUIB/>
</div>);
*/

// Way B
return (
<div>
  {MyUIA()}
  {MyUIB()}
</div>);


}

The results of render by WayA and WayB is the same, but in first case there is a flickering WayA 和 WayB 渲染的结果是一样的,但是第一种情况有闪烁

  1. So why the WayB has better performance without flickering?那么为什么 WayB 的性能更好而没有闪烁呢?
  2. What is the best way to render part of renders thas exist in the same function component?渲染同一 function 组件中存在的部分渲染的最佳方法是什么?
  3. does WayA and WayB have a specific name for example "render by calling native method" and "render by calling JSX method" in react? WayA 和 WayB 是否有特定的名称,例如“通过调用本机方法渲染”和“通过调用 JSX 方法渲染”?

Since as we know every code we write in react is converted into JSX first, then it get rendered so if we see for native method the JSX element is created twice while for JSX it's created only once.因为我们知道我们在 react 中编写的每个代码都会首先转换为 JSX,然后才会被渲染,所以如果我们看到原生方法的 JSX 元素被创建了两次,而 JSX 的元素只创建了一次。 So this is the case one can see the flickering.所以这是可以看到闪烁的情况。 I hope it helps.我希望它有所帮助。

function myMainComponent() { 
    const MyUIA = () => {
       return /*#__PURE__*/React.createElement("div", null,/*#__PURE__*/React.createElement(ComponentA, null)); };

     const MyUIB = () => {
       return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(ComponentA, null));}; 
      
      // Way A
       return /*#__PURE__*/React.createElement("div", null, 
              /*#__PURE__*/React.createElement(MyUIA, null), 
              /*#__PURE__*/React.createElement(MyUIB, null)); 
      
      // Way B
      return /*#__PURE__*/React.createElement("div", null, MyUIA(), MyUIB());
}

This might not be relevant to your problem but it is a general tip for all react users that a react component must have only 1 return statement at most这可能与您的问题无关,但它是所有反应用户的一般提示,反应组件最多只能有 1 个返回语句

暂无
暂无

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

相关问题 如何从渲染功能中分离JSX部件 - How to separate the JSX part from render function 利用 ! 在 Reactjs JSX 中的组件 - use ! in Reactjs Component in JSX 是否可以将不完整的jsx片段插入另一个jsx文件的渲染函数中? - Is it possible to insert an incomplete jsx snippet into a render function of another jsx file? ReactJS + FineUploader:如果模板使用JSX驻留在render()函数中,则会出错 - ReactJS + FineUploader: Error if template resides in render() function using JSX ReactJS,如何在另一个组件的render方法中渲染一个组件? - ReactJS, how to render a component within another component's render method? 如何从父组件中的子组件呈现功能的jsx - how to render jsx of function from child component in parent component 如何在不使用 JSX 的情况下编写 ReactJS 函数组件? - How to write ReactJS function component without using JSX? function 组件的一部分不会在 React 中呈现,即使相同的代码适用于另一个组件 - Part of function Component wont render in React even though same code works for another component 如何使用功能组件将数据从一个组件传递到另一个组件,而无需重新渲染或 jsx,例如 function - How to pass data from one component to another in react without re-render or jsx like from a function using functional components 如何从函数中呈现JSX组件,以使其在屏幕上显示 - How to render a JSX component from the function show that it dispaly on screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM