Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a React component vs. a React element?

React.isValidElement tests true for both React components as well as React elements. How would I test, specifically, that an object is a React component? Currently, I'm doing it by testing typeof obj.type === 'function', but I'm hoping there's a better way.

like image 640
jedmao Avatar asked Oct 18 '15 15:10

jedmao


People also ask

What is the difference between React component and React element?

A React element is an object representation of a DOM node. A component encapsulates a DOM tree. Elements are immutable i,e once created cannot be changed. The state in a component is mutable.

What React API function allows you to check whether the object is a react element?

React. isValidElement tests true for both React components as well as React elements.

What is the difference between element and component?

There is certainly a difference between elements and components. Furthermore, a component refers to a small part of a larger entity that mostly is a manufactured object. In contrast, an element is one the simplest parts of which anything consists of.

How does React differentiate between custom made components and normal HTML elements?

React Elements With a function component, this element is the object that the function returns. React elements are not what we see in the browser. They are just objects in memory and we can't change anything about them. React elements can have other type properties other than native HTML elements.

What is an element in react programming?

React Elements 1 A React Element is what gets returned from components. ... 2 With a function component, this element is the object that the function returns. 3 With a class component, the element is the object that the component’s render function returns. ... 4 React elements are not what we see in the browser. ... More items...

What is the difference between class component and function component in react?

With a function component, this element is the object that the function returns. With a class component, the element is the object that the component’s render function returns. R React elements are not what we see in the browser. They are just objects in memory and we can’t change anything about them.

How does react know what an element is being rendered to?

When React sees an element with a function or class type, it knows to ask that component what element it renders to, given the corresponding props. When it sees this element:

What is the difference between react element and object?

A react element describes what we want to see on the screen. A React element is an object representation of a DOM node. _It's important to make this distinction here because the element is not the actual thing we see on the screen, rather the object representation is what is rendered.


2 Answers

If you really want to type check for

  • component vs element

  • class vs functional component

  • DOM vs Composite Element

You could try something like this.

function isClassComponent(component) {
    return (
        typeof component === 'function' && 
        !!component.prototype.isReactComponent
    )
}

function isFunctionComponent(component) {
    return (
        typeof component === 'function' && 
        String(component).includes('return React.createElement')
    )
}

function isReactComponent(component) {
    return (
        isClassComponent(component) || 
        isFunctionComponent(component)
    )
}

function isElement(element) {
    return React.isValidElement(element);
}

function isDOMTypeElement(element) {
    return isElement(element) && typeof element.type === 'string';
}

function isCompositeTypeElement(element) {
    return isElement(element) && typeof element.type === 'function';
}

USE

// CLASS BASED COMPONENT
class Foo extends React.Component {
  render(){
      return <h1>Hello</h1>;
  }
}

const foo = <Foo />;

//FUNCTIONAL COMPONENT
function Bar (props) { return <h1>World</h1> }
const bar = <Bar />;

// REACT ELEMENT
const header = <h1>Title</h1>;

// CHECK
isReactComponent(Foo); // true
isClassComponent(Foo); // true
isFunctionComponent(Foo); // false
isElement(Foo); // false

isReactComponent(<Foo />) // false
isElement(<Foo />) // true
isDOMTypeElement(<Foo />) // false
isCompositeTypeElement(<Foo />) // true

isReactComponent(Bar); // true
isClassComponent(Bar); // false
isFunctionComponent(Bar); // true
isElement(Bar); // false

isReactComponent(<Bar />) // false
isElement(<Bar />) // true
isDOMTypeElement(<Bar />) // false
isCompositeTypeElement(<Bar />) // true

isReactComponent(header); // false
isElement(header); // true
isDOMTypeElement(header) // true
isCompositeTypeElement(header) // false

Example Codepen

like image 91
Emanual Jade Avatar answered Oct 16 '22 14:10

Emanual Jade


The simplest solution is:

React.isValidElement(element)
like image 42
Jackkobec Avatar answered Oct 16 '22 14:10

Jackkobec