Skip to content
Draft
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
8 changes: 2 additions & 6 deletions compat/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
useInsertionEffect,
useTransition
} from './hooks';
import { memo } from './memo';
import { memo, REACT_MEMO_SYMBOL } from './memo';
import { createPortal } from './portals';
import {
REACT_ELEMENT_TYPE,
Expand Down Expand Up @@ -77,11 +77,7 @@ function isFragment(element) {
* @returns {boolean}
*/
function isMemo(element) {
return (
!!element &&
typeof element.displayName == 'string' &&
element.displayName.indexOf('Memo(') == 0
);
return !!element && element.$$typeof === REACT_MEMO_SYMBOL;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions compat/src/memo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createElement } from 'preact';
import { shallowDiffers } from './util';

export const REACT_MEMO_SYMBOL = Symbol.for('react.memo');

/**
* Memoize a component, so that it only updates when the props actually have
* changed. This was previously known as `React.pure`.
Expand All @@ -27,5 +29,8 @@ export function memo(c, comparer) {
Memoed.displayName = 'Memo(' + (c.displayName || c.name) + ')';
Memoed.prototype.isReactComponent = true;
Memoed.type = c;
// Libraries (e.g. react-is) detect memo components via this marker,
// mirroring React's `Symbol.for('react.memo')` and our `forwardRef`.
Memoed.$$typeof = REACT_MEMO_SYMBOL;
return Memoed;
}
2 changes: 1 addition & 1 deletion compat/test/browser/forwardRef.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('forwardRef', () => {
expect(App.prototype.isReactComponent).to.equal(true);
});

it.skip('should have $$typeof property', () => {
it('should have $$typeof property', () => {
let App = forwardRef((_, ref) => <div ref={ref}>foo</div>);
const expected = getSymbol('react.forward_ref', 0xf47);
expect(App.$$typeof).to.equal(expected);
Expand Down
8 changes: 8 additions & 0 deletions compat/test/browser/isMemo.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ describe('isMemo', () => {
expect(isMemo(preactCreateElement(Fragment, {}))).to.equal(false);
});

it('should not detect a non-memo component with a Memo displayName', () => {
function Foo() {
return <h1>Hello World</h1>;
}
Foo.displayName = 'Memo(Foo)';
expect(isMemo(Foo)).to.equal(false);
});

it('should detect a compat vnode as false', () => {
expect(isMemo(React.createElement(Fragment, {}))).to.equal(false);
});
Expand Down
8 changes: 8 additions & 0 deletions compat/test/browser/memo.test.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getSymbol } from './testUtils';
import { setupRerender } from 'preact/test-utils';
import {
createEvent,
Expand Down Expand Up @@ -37,6 +38,13 @@ describe('memo()', () => {
expect(App.prototype.isReactComponent).to.equal(true);
});

it('should have $$typeof property', () => {
// eslint-disable-next-line react/display-name
let App = memo(() => <div>foo</div>);
const expected = getSymbol('react.memo', 0xead3);
expect(App.$$typeof).to.equal(expected);
});

it('should work with function components', () => {
let spy = vi.fn();

Expand Down