cyan33 / learn-react-source-code

Build react from scratch (code + blog)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

关于Day1-Guidance里面有一点小疑惑

lazysheep666 opened this issue · comments

由于我们在使用 React 实现组件化的时候,使用的有且只有 CompositeComponent,所以,我们的每一个组件,其实都是一个 Component。但是当 React 试图去 render 这些组件的时候,会将 Element 转化成 Component,进而转化成真正的 DOM 节点。

这里为什么不是将Component转化为Element因为看到React Components, Elements, and Instancestype为function或者class的element最终都会转换为type为string的element
如理解有误请大佬指出。

Hey @lazysheep666,

That's a good question. Sorry for not having made it clear in the first place.

I think both are right, depends on how you understand the flow of rendering.

When I try to render <App> (element), I need to instantiate the class App (component). And when after that we try to render its children components, we go to its render method and build the element tree. And then we repeat the process.

So it's actually:

element -> component -> element -> component -> element ...

Hope this answers your question.

Thanks @cyan33 :)