Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/react-is/src/ReactIs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import {
REACT_CONTEXT_TYPE,
REACT_ELEMENT_TYPE,
REACT_LEGACY_ELEMENT_TYPE,
REACT_FORWARD_REF_TYPE,
REACT_FRAGMENT_TYPE,
REACT_LAZY_TYPE,
Expand Down Expand Up @@ -42,6 +43,7 @@ export function typeOf(object: any): mixed {
const $$typeof = object.$$typeof;
switch ($$typeof) {
case REACT_ELEMENT_TYPE:
case REACT_LEGACY_ELEMENT_TYPE:
const type = object.type;

switch (type) {
Expand Down Expand Up @@ -140,7 +142,8 @@ export function isElement(object: any): boolean {
return (
typeof object === 'object' &&
object !== null &&
object.$$typeof === REACT_ELEMENT_TYPE
(object.$$typeof === REACT_ELEMENT_TYPE ||
object.$$typeof === REACT_LEGACY_ELEMENT_TYPE)
);
}
export function isForwardRef(object: any): boolean {
Expand Down
15 changes: 15 additions & 0 deletions packages/react-is/src/__tests__/ReactIs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let React;
let ReactDOM;
let ReactIs;
let SuspenseList;
let ReactSymbols;

describe('ReactIs', () => {
beforeEach(() => {
Expand All @@ -21,6 +22,7 @@ describe('ReactIs', () => {
React = require('react');
ReactDOM = require('react-dom');
ReactIs = require('react-is');
ReactSymbols = require('shared/ReactSymbols');

if (gate(flags => flags.enableSuspenseList)) {
SuspenseList = React.unstable_SuspenseList;
Expand Down Expand Up @@ -116,6 +118,19 @@ describe('ReactIs', () => {
expect(ReactIs.isElement(<React.Suspense />)).toBe(true);
});

it('should identify legacy elements from older React versions', () => {
// Simulate a legacy React element with the old $$typeof symbol
const legacyElement = {
$$typeof: ReactSymbols.REACT_LEGACY_ELEMENT_TYPE,
type: 'div',
key: null,
ref: null,
props: {},
};
expect(ReactIs.isElement(legacyElement)).toBe(true);
expect(ReactIs.typeOf(legacyElement)).toBe(ReactIs.Element);
});

it('should identify ref forwarding component', () => {
const RefForwardingComponent = React.forwardRef((props, ref) => null);
expect(ReactIs.isValidElementType(RefForwardingComponent)).toBe(true);
Expand Down