npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

is-react

v1.5.4

Published

Determine if a variable or expression is a React element or component

Downloads

46,504

Readme

Is React

A library to determine if a variable or an expression is a React element or component. For a more thorough understanding, this article describes elements and components in React, and this article gives an understanding of the JSX syntax.

Sponsored by:

Install

yarn add is-react or npm i --save is-react to use the package.

Examples

Real world:

import React from 'react';
import isReact from 'is-react';

const MyImageComponent = ({ SomeProp }) => {
  if (typeof SomeProp === 'string') {
    // assume it's the src for an image
    return <img src={SomeProp} />;
  } else if (isReact.component(SomeProp)) {
    return <SomeProp />;
  } else if (isReact.element(SomeProp)) {
    return SomeProp;
  }

  return null;
};

Samples:

// Class 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
isReact.compatible(Foo); // true
isReact.component(Foo); // true
isReact.classComponent(Foo); // true
isReact.functionComponent(Foo); // false
isReact.element(Foo); // false

isReact.compatible(<Foo />); // true
isReact.component(<Foo />); // false
isReact.element(<Foo />); // true
isReact.DOMTypeElement(<Foo />); // false
isReact.compositeTypeElement(<Foo />); // true

isReact.compatible(Bar); // true
isReact.component(Bar); // true
isReact.classComponent(Bar); // false
isReact.functionComponent(Bar); // true
isReact.element(Bar); // false

isReact.compatible(<Bar />); // true
isReact.component(<Bar />); // false
isReact.element(<Bar />); // true
isReact.DOMTypeElement(<Bar />); // false
isReact.compositeTypeElement(<Bar />); // true

isReact.compatible(header); // true
isReact.component(header); // false
isReact.element(header); // true
isReact.DOMTypeElement(header); // true
isReact.compositeTypeElement(header); // false

API

import isReact from 'is-react' to use the package

All functions return a boolean. The primary functions you will most likely use are compatible(), element(), and component().

isReact.compatible()

Determine if a variable or expression is compatible with React. Valid React components and elements return true.

isReact.element()

Determine if a variable or expression is a React element. Will return true for both DOM type and Composite type components.

isReact.component()

Determine if a variable or expression is a React component. Will return true for both functional and class components.

isReact.classComponent()

Determine if a variable or expression is a React class component.

isReact.functionComponent()

Determine if a variable or expression is a React functional component.

isReact.DOMTypeElement()

Determine if a variable or expression is a React DOM type element.

isReact.compositeTypeElement()

Determine if a variable or expression is a React Composite type element.

Thanks!

Inspired by this Stackoverflow answer