简体   繁体   English

如何反应才能识别它是 class 组件还是功能组件?

[英]How react can Identify it's a class component or functional component?

I have created two component in my React App one is class component and another one is functional component.我在我的 React App 中创建了两个组件,一个是 class 组件,另一个是功能组件。 But how react can identify when it call the component it's a class component or functional component?但是当它调用组件时,react 如何识别它是 class 组件还是功能组件?

It's a question ask by Interviewer这是面试官提出的问题

But how react can identify when it calls the component it's a class component or functional component?但是当它调用组件时,react 如何识别它是 class 组件还是功能组件?

// Who is a class component and who is a functional component?
ReactDOM.render(
  <>
    <ComponentA />
    <ComponentB />
    {React.createElement(ComponentA)}
    {React.createElement(ComponentB)}
  </>,
  document.getElementById('root')
);

While JSX represents objects , as stated in docs they are equivalent:虽然JSX 代表 objects ,如文档中所述,它们是等价的:

The above two components are equivalent from React's point of view.从 React 的角度来看,上述两个组件是等价的。

But actually , React checking isReactComponent flag to determinate if it's a class component or function component:实际上,React 检查isReactComponent标志以确定它是 class 组件还是 function 组件:

// @ ReactComponent.js
ReactComponent.prototype.isReactComponent = {};

// @ ReactFiber.js
function shouldConstruct(Component: Function) {
  const prototype = Component.prototype;
  return !!(prototype && prototype.isReactComponent);
}

For more in-depth explanation check Dan Abramov's blog post .有关更深入的解释,请查看Dan Abramov 的博客文章

React check the component for the isReactComponent flag (which is present on React.Component.prototype source ) to determine weather it's a class or a function. React 检查组件的isReactComponent标志(存在于React.Component.prototype 上)以确定天气它是 class 还是 function。

You can read about it in detail here: https://overreacted.io/how-does-react-tell-a-class-from-a-function/您可以在此处详细了解它: https://overreacted.io/how-does-react-tell-a-class-from-a-function/

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

相关问题 我如何编写此类组件来响应功能组件。? - How can i write this class component to react functional component.? 反应:如何访问 class 组件中的功能组件的道具 - React: How can I acces to props of a functional component in a class component 我怎样才能把这个 React class 组件变成一个功能组件? - How can i turn this React class component into a functional component? 将类组件反应为功能组件 - React class component to functional component 如何编写返回 class 组件作为功能组件的 React 组件? - How can I write React component that returns a class component as a functional component? 将 React 类组件的状态迁移到功能组件 - Migrating a React class component's state to a functional component 如何在反应中将类组件作为功能道具传递? - How to pass class component as a functional prop in react? 如何将功能性React组件绑定到类事件 - How to bind functional React component to class events 我们如何在 react-native 功能组件中声明和使用 class 而不用 React.Component 扩展它? - How can we declare and use class in react-native functional component without extending it with React.Component? 将 React Class 组件转换为 React 函数式组件 - Converting React Class Component to a React Functional Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM