简体   繁体   English

DOM 中的 React 组件渲染

[英]React Component rendering in DOM

1) When you build an app with create-react-app will all the code in every component's render method be packaged into one file and loaded all at once to the DOM initially? 1) 当您使用 create-react-app 构建应用程序时,每个组件的 render 方法中的所有代码都会被打包到一个文件中并最初一次全部加载到 DOM 中吗? Or are they loaded when they are needed?还是在需要时加载它们? If so, what components are loaded early and which are loaded later?如果是这样,哪些组件是提前加载的,哪些是稍后加载的?

2) If everything is loaded all at once when it is injected into the root node via the React.render() method, then what is the point of the lifecycle method componentDidMount? 2)如果所有东西都是通过React.render()方法注入根节点时一次性加载的,那么生命周期方法componentDidMount的意义何在? Wouldn't everything mount at the same time if it is all just getting injected together and becoming part of the DOM tree on the initial load?如果它们只是被注入在一起并在初始加载时成为 DOM 树的一部分,难道不是所有东西都同时挂载吗?

Create-React-app gives you a powerful preset configurations so you dont need to config react project from scratch. Create-React-app 为您提供了强大的预设配置,因此您无需从头开始配置 react 项目。 You don't have to set webpack, default packages, lint configuration, folder structure and so on.您不必设置 webpack、默认包、lint 配置、文件夹结构等。 Eventually you can make changes if you feel comfortable.最终,如果你觉得舒服,你可以做出改变。 Or you can even start from scratch, but that can be challenging when you are a beginner.或者您甚至可以从头开始,但是当您是初学者时,这可能具有挑战性。

Components in react are created and imported as needed starting by a root point at html file. react 中的组件是根据需要从 html 文件的根点开始创建和导入的。 The root entrance point will serve as reference for react to create an entrance point and from it will build the virtual DOM and create other components as needed, not every every thing at once.根入口点将作为 react 创建入口点的参考,并从中构建虚拟 DOM 并根据需要创建其他组件,而不是同时创建所有内容。

Meanwhile, Webpack compiles all files and directories into a single file based on the entrance file which calls the rest.同时,Webpack以调用rest的入口文件为基础,将所有文件和目录编译成一个文件。 Without webpack, or similar, you wouldnt have a nice folder structure to develop you react project.如果没有 webpack 或类似的,您将不会有一个很好的文件夹结构来开发您的反应项目。

From there on, react handles a virtual DOM, to manage components state and props updates through component lifecycicles, making proper comparison to avoid unnecessary rerenders, and remove components if needed.从那里开始,react 处理一个虚拟 DOM,管理组件 state 和通过组件生命周期更新道具,进行适当的比较以避免不必要的重新渲染,并在需要时删除组件。 If everything was rendered at once you would have static file which would make the use of react, or similar spa, unnecessary.如果所有内容都立即渲染,您将拥有 static 文件,这将不需要使用 react 或类似的 spa。

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

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