@formepdf/react 0.9.0 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/serialize.js +33 -3
- package/package.json +1 -1
package/dist/serialize.js
CHANGED
|
@@ -66,13 +66,43 @@ function isDocumentType(type) {
|
|
|
66
66
|
function resolveElement(element) {
|
|
67
67
|
let resolved = element;
|
|
68
68
|
for (let i = 0; i < 10 && typeof resolved.type === 'function' && !isDocumentType(resolved.type); i++) {
|
|
69
|
-
const result = resolved.type
|
|
69
|
+
const result = callComponent(resolved.type, resolved.props);
|
|
70
70
|
if (!isValidElement(result))
|
|
71
71
|
break;
|
|
72
72
|
resolved = result;
|
|
73
73
|
}
|
|
74
74
|
return resolved;
|
|
75
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Call a function component directly (outside React's render cycle).
|
|
78
|
+
* React Compiler injects `useMemoCache` hooks into compiled components,
|
|
79
|
+
* which throws when called outside a render context. We catch that and
|
|
80
|
+
* give a clear error message pointing to the `'use no memo'` directive.
|
|
81
|
+
*/
|
|
82
|
+
function callComponent(fn, props) {
|
|
83
|
+
try {
|
|
84
|
+
return fn(props);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
if (err instanceof Error && /hook|useMemoCache|Invalid hook call/i.test(err.message)) {
|
|
88
|
+
const name = fn.displayName || fn.name || 'Unknown';
|
|
89
|
+
throw new Error(`Component "${name}" appears to be compiled by React Compiler, which injects hooks ` +
|
|
90
|
+
`that cannot run outside of React's render cycle. Forme's serialize() calls components ` +
|
|
91
|
+
`directly to walk the element tree.\n\n` +
|
|
92
|
+
`Fix: Add 'use no memo' at the top of the component to opt it out of React Compiler:\n\n` +
|
|
93
|
+
` function ${name}(props) {\n` +
|
|
94
|
+
` 'use no memo';\n` +
|
|
95
|
+
` return <Document>...</Document>;\n` +
|
|
96
|
+
` }\n\n` +
|
|
97
|
+
`Alternatively, call the component yourself before passing to renderDocument():\n\n` +
|
|
98
|
+
` const element = <${name} data={data} />;\n` +
|
|
99
|
+
` // becomes:\n` +
|
|
100
|
+
` const element = ${name}({ data });\n` +
|
|
101
|
+
` await renderDocument(element);`);
|
|
102
|
+
}
|
|
103
|
+
throw err;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
76
106
|
// ─── Public API ──────────────────────────────────────────────────────
|
|
77
107
|
/**
|
|
78
108
|
* Serialize a React element tree into a Forme JSON document object.
|
|
@@ -309,7 +339,7 @@ function serializeChild(child, parent = null) {
|
|
|
309
339
|
}
|
|
310
340
|
// Unknown component — try to call it as a function component
|
|
311
341
|
if (typeof element.type === 'function') {
|
|
312
|
-
const result = element.type
|
|
342
|
+
const result = callComponent(element.type, element.props);
|
|
313
343
|
if (isValidElement(result)) {
|
|
314
344
|
return serializeChild(result, parent);
|
|
315
345
|
}
|
|
@@ -1643,7 +1673,7 @@ function serializeTemplateChild(child, parent = null) {
|
|
|
1643
1673
|
}
|
|
1644
1674
|
// Unknown function component — call it
|
|
1645
1675
|
if (typeof element.type === 'function') {
|
|
1646
|
-
const result = element.type
|
|
1676
|
+
const result = callComponent(element.type, element.props);
|
|
1647
1677
|
if (isValidElement(result)) {
|
|
1648
1678
|
return serializeTemplateChild(result, parent);
|
|
1649
1679
|
}
|