cyan33 / learn-react-source-code

Build react from scratch (code + blog)
208 stars 23 forks source link

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

Closed lazysheep666 closed 5 years ago

lazysheep666 commented 5 years ago

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

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

cyan33 commented 5 years ago

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.

lazysheep666 commented 5 years ago

Thanks @cyan33 :)