首页 > 解决方案 > Why we do extends React.Component when creating the class component in React?

问题描述

I am new to React and i am very confused and continuously thinking about it day and night and almost all my hairs are fallen and still falling down, that why we always extends React.Component class when creating the component with the class.

But in the functional component and in other methods we do not do any such thing to push the created component into the React.Component class. So the main question is that What is React.Component class why we always add new components to it as a child using the extends and why we cannot create components in react native without using the extends in class component or without extending the React.Component class. So, what is React.Component class whats going on inside React under the hood, whats the scenario ?

I will be very very thankful to the person who will answer my question with examples and easy approach.

标签: javascriptreactjsreact-nativees6-class

解决方案


React 组件只是简单的函数。

例如,只要不使用 JSX,就可以在不使用reactimport 的情况下渲染功能组件。react

如果您想使用 JSX 并将您的文件写入您HTMLjs文件,那么您需要导入react.

如果您想编写一个利用生命周期方法的组件,您将需要扩展默认Component类。该类Component包含生命周期方法,不应与其他通用react组件混淆,例如功能组件或您可能编写的任何组件。这Component指的是由react.

扩展Component将使您能够访问诸如componentDidMountcomponentDidUpdate和.componentWillUnmountrender


推荐阅读