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

前端 未结 7 1973
广开言路
广开言路 2020-12-02 10:04

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? Curren

7条回答
  •  难免孤独
    2020-12-02 10:07

    This is an old question, with an old answer.

    If you run into this issue, probably you'll want to check the react-is NPM package page: https://www.npmjs.com/package/react-is

    It's an official React module that takes into account details like ref forwarding, and memo to check for an element type.

    To check if a value is an element type do: ReactIs.isValidElementType(obj)

提交回复
热议问题