javascript - 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.
解决方案
React 组件只是简单的函数。
例如,只要不使用 JSX,就可以在不使用react
import 的情况下渲染功能组件。react
如果您想使用 JSX 并将您的文件写入您HTML
的js
文件,那么您需要导入react
.
如果您想编写一个利用生命周期方法的组件,您将需要扩展默认Component
类。该类Component
包含生命周期方法,不应与其他通用react
组件混淆,例如功能组件或您可能编写的任何组件。这Component
指的是由react
.
扩展Component
将使您能够访问诸如componentDidMount
、componentDidUpdate
和.componentWillUnmount
render
推荐阅读
- umbraco - 允许用户在 Umbraco 输入控件的内容中输入法语、西班牙语和韩语
- javascript - 如何在异步函数中访问双嵌套数组中的数据 - javascript?
- hive - 如何在没有写权限的情况下使用 hive 将数据库表数据从 hdfs 导出到本地 csv
- python - Tensorflow - 如何将 int32 转换为字符串(使用 Python API for Tensorflow)
- apache-spark - DCOS 集群上的 Spark 提交失败并出现 java.net.UnknownHostException: hdfs
- php - 按 ID 排序帖子属于多个关系
- sql - 如何找到最少的学生群体
- javascript - 无法连接到节点 js 中的 websocket
- c# - 使用依赖注入 unity Config 的简单授权服务提供者
- javascript - div上的ajax调用问题