Skip to content

Commit c925872

Browse files
rubennortefacebook-github-bot
authored andcommitted
Throw an error when calling root.render outside of a task (#49003)
Summary: Pull Request resolved: #49003 Changelog: [internal] I was adding a benchmark for rendering thousands of views and it was surprisingly fast, until I realized I wasn't wrapping the call to `root.render` in `runTask`, which means the benchmark wasn't really doing the rendering, only scheduling a microtask that was never executed. This is a safety mechanism to prevent those mistakes. Reviewed By: sammy-SC Differential Revision: D68771170 fbshipit-source-id: 5bd8e6ba9e1168db2320572c99b3a01ebd6aeeed
1 parent 32fe244 commit c925872

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/react-native-fantom/src/__tests__/Fantom-itest.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ describe('Fantom', () => {
155155
},
156156
);
157157
});
158+
159+
it('throws when trying to render a root outside of a task', () => {
160+
const root = Fantom.createRoot();
161+
162+
expect(() => {
163+
root.render(<View />);
164+
}).toThrow(
165+
'Unexpected call to `render` outside of the event loop. Please call `render` within a `runTask` callback.',
166+
);
167+
168+
expect(() => {
169+
Fantom.runTask(() => {
170+
root.render(<View />);
171+
});
172+
}).not.toThrow();
173+
});
158174
});
159175

160176
describe('getRenderedOutput', () => {

packages/react-native-fantom/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ class Root {
5757
globalSurfaceIdCounter += 10;
5858
}
5959

60-
render(element: MixedElement) {
60+
render(element: MixedElement): void {
61+
if (!flushingQueue) {
62+
throw new Error(
63+
'Unexpected call to `render` outside of the event loop. Please call `render` within a `runTask` callback.',
64+
);
65+
}
66+
6167
if (!this.#hasRendered) {
6268
NativeFantom.startSurface(
6369
this.#surfaceId,

0 commit comments

Comments
 (0)