Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for React 18 #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
14 changes: 7 additions & 7 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
},
"sideEffects": false,
"devDependencies": {
"@types/react-dom": "^17.0.0",
"react": "^17.0.0",
"react-dom": "^17.0.0"
"@types/react-dom": "^18.0.5",
"react": "^18.1.0",
"react-dom": "^18.1.0"
},
"dependencies": {
"dependencies": {
"@remote-ui/async-subscription": "^2.1.13",
"@remote-ui/core": "^2.1.16",
"@remote-ui/rpc": "^1.3.4",
"@types/react": ">=17.0.0 <18.0.0",
"@types/react-reconciler": "^0.26.0",
"react-reconciler": ">=0.26.0 <0.27.0"
"@types/react": "^18.0.9",
"@types/react-reconciler": "^0.26.7",
"react-reconciler": "^0.28.0"
}
}
20 changes: 17 additions & 3 deletions packages/react/src/reconciler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export type Reconciler = ReactReconciler<
PublicInstance
>;

const scheduleMicrotask = (callback: () => void) =>
typeof queueMicrotask === 'function'
? queueMicrotask
: Promise.resolve(null).then(callback).catch(handleErrorInNextTick);

export const reconciler = reactReconciler<
Type,
Props,
Expand All @@ -48,9 +53,18 @@ export const reconciler = reactReconciler<
scheduleTimeout: setTimeout,
cancelTimeout: clearTimeout,
noTimeout: false,
// @see https://github.com/facebook/react/blob/master/packages/react-dom/src/client/ReactDOMHostConfig.js#L408
queueMicrotask: (callback) =>
Promise.resolve(null).then(callback).catch(handleErrorInNextTick),

// Microtask scheduling
// @see https://github.com/facebook/react/blob/2c8a1452b82b9ec5ebfa3f370b31fda19610ae92/packages/react-dom/src/client/ReactDOMHostConfig.js#L391-L401
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - types in `@types/react-reconciler` are outdated
supportsMicrotasks: true,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - types in `@types/react-reconciler` are outdated
scheduleMicrotask,

// Compat for React <= 17.x
queueMicrotask: scheduleMicrotask,

isPrimaryRenderer: true,
supportsMutation: true,
Expand Down
24 changes: 21 additions & 3 deletions packages/react/src/render.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {ReactElement} from 'react';
import {version} from 'react';
import type {RemoteRoot} from '@remote-ui/core';
import type {RootTag} from 'react-reconciler';

Expand All @@ -15,8 +16,8 @@ const cache = new WeakMap<
}
>();

// @see https://github.com/facebook/react/blob/993ca533b42756811731f6b7791ae06a35ee6b4d/packages/react-reconciler/src/ReactRootTags.js
// I think we are a legacy root?
// @see https://github.com/facebook/react/blob/fea6f8da6ab669469f2fa3f18bd3a831f00ab284/packages/react-reconciler/src/ReactRootTags.js#L12
// We don't support concurrent rendering for now.
const LEGACY_ROOT: RootTag = 0;

export function render(
Expand All @@ -28,9 +29,26 @@ export function render(
let cached = cache.get(root);

if (!cached) {
const major = Number(version.split('.')?.[0] || 18);

// Since we haven't created a container for this root yet, create a new one
const value = {
container: reconciler.createContainer(root, LEGACY_ROOT, false, null),
container:
major >= 18
? reconciler.createContainer(
root,
LEGACY_ROOT,
null,
false,
null,
// Might not be necessary
'r-ui',
() => null,
null,
)
: // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - TS doesn't support multiple type versions of the same package
reconciler.createContainer(root, LEGACY_ROOT, false, null),
// We also cache the render context to avoid re-creating it on subsequent render calls
renderContext: {root, reconciler},
};
Expand Down
43 changes: 27 additions & 16 deletions packages/react/src/tests/e2e.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint @shopify/jsx-no-hardcoded-content: off */

import {useEffect, useContext, createContext, useState} from 'react';
import {render as domRender} from 'react-dom';
import {createRoot} from 'react-dom/client';
import type {Root} from 'react-dom/client';
import {act as domAct, Simulate} from 'react-dom/test-utils';
import {
KIND_ROOT,
Expand All @@ -18,6 +19,10 @@ import {
ReactPropsFromRemoteComponentType,
} from '..';

// Tell react to enable `act()` behaviour. See:
// https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#configuring-your-testing-environment
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;

const RemoteHelloWorld = createRemoteReactComponent<
'HelloWorld',
{name: string | RemoteFragment}
Expand Down Expand Up @@ -97,14 +102,21 @@ function HostWithFragment({

describe('@remote-ui/react', () => {
let appElement!: HTMLElement;
let domRoot: Root;

beforeEach(() => {
appElement = document.createElement('div');
document.body.appendChild(appElement);
domAct(() => {
domRoot = createRoot(appElement);
});
jest.useFakeTimers();
});

afterEach(() => {
domAct(() => {
domRoot.unmount();
});
appElement.remove();
jest.useRealTimers();
});
Expand All @@ -128,7 +140,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -168,7 +180,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -200,11 +212,10 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(
domRoot.render(
<PersonContext.Provider value={person}>
<HostApp />
</PersonContext.Provider>,
appElement,
);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
Expand Down Expand Up @@ -234,7 +245,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -262,7 +273,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -317,7 +328,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -388,7 +399,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -457,7 +468,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -519,7 +530,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -581,7 +592,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -641,7 +652,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -708,7 +719,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -777,7 +788,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down Expand Up @@ -833,7 +844,7 @@ describe('@remote-ui/react', () => {
}

domAct(() => {
domRender(<HostApp />, appElement);
domRoot.render(<HostApp />);
render(<RemoteApp />, remoteRoot, () => {
remoteRoot.mount();
});
Expand Down
Loading