Open gabrielvincent opened 3 years ago
Thanks @gabrielvincent ! and sorry :( I'll try to see this asap
I'm experiencing a similar issue with dynamic rendering in the newest version (2.0.8). Rendering dynamic content of a Text
component causes the PDF to be re-rendered over and over again nonstop. This did not occur in v1.x
Same issue by my side. In order to stop the infinite re-rendering, needed to unmount the footer component which manage the dynamic page number. This happened upgrading to v 2.x.
Here is the component who is causing the issue:
import React from "react";
import {Text, StyleSheet } from "@react-pdf/renderer";
const styles = StyleSheet.create({
pageNumber: {
position: "absolute",
fontSize: 12,
bottom: 30,
left: 0,
right: 0,
textAlign: "center",
color: "grey",
fontFamily: "Nunito",
},
});
export default () => (
<Text
style={styles.pageNumber}
render={({ pageNumber, totalPages }) => `${pageNumber} / ${totalPages}`}
fixed
/>
);
I'm also seeing this issue with dynamic rendering in a WebvView (via BlobProvider
). I get into an infinite loop with:
<Document>
<Page size="A4" orientation="landscape">
<Text
render={({ pageNumber, totalPages }) => {
console.log('render', pageNumber, totalPages);
return `${pageNumber} / ${totalPages}`;
}}
fixed
/>
</Page>
</Document>
I'll see if I can dig through the code a bit and find a cause
@diegomura any update on this issue this is breaking all our pdfs right now.
I noticed that you can wrap render
function with useCallback
as a workaround to prevent infinite re-renders, e.g.
const pageNumbers = useCallback(({ pageNumber, totalPages }) => {
return `${pageNumber} / ${totalPages}`;
}, []);
<Text render={pageNumbers} />
This is a brilliant solution. Do you mind explaining a little more about how you came to this solution and more elaborate more on how this actually worked?
Either I'm missing something or this was never fixed, but I cannot get any dynamic content to render using using the render prop or calling jsx expressions within Text or View components. I keep getting the same TypeError: element.type is not a function as mentioned in the OP. Was there any resolution on this?
Describe the bug
View
whose content was rendered using therender
prop function used to be rendered correctly onreact-pdf
v1 even if one of the sub-View
s returned as its children was aText
that uses therender
prop to render its content as well.react-pdf
v1 the return of therender
prop function of aView
could be aReact.Fragment
with as manyView
s as wanted. Now an error (TypeError: element.type is not a function
) is thrown if the return of therender
function is not a singleView
. This can be seen happening here on the REPL.In this issue I'll use the
Footer
component of my project for code examples. This is how this footer looks like. Note that the layout of the footer changes depending on the page number: it's aligned to the right on even pages and to the left on even pages. That's why I need to use therender
prop function of aView
and return in it aText
that is also rendered using its ownrender
prop function. ThepageNumber
in theView
is used to determine the layout of the footer.The reason why I need to also render the content of
Text
using therender
prop function is because onlyText
can give metotalPages
, which I use as a flag to differentiate between the first render from the last one, when all the pages are already at their final position. I can then properly instruct myPageTracker
class about keeping a count of page numbers and at what page each chapter starts. This is crucial in order to enable a Table of Contents.Here's the
Footer
component's code which works onreact-pdf
v1.6.14:On
react-pdf
v2.0.4 this code produces theTypeError: element.type is not a function
.I can stop that error from happening if I replace the
Fragment
withreact-pdf
'sView
primitive:However, this is the result I get from the code above:
A much simpler example of this can be seen here on the REPL. After some tests, it became evident that even a simple dynamic render is not working. In this example on the REPL I could not render a simple
View
as the return of therender
prop function. I expected to see aText
with a green background inside a blueView
inside a redView
. Only the red, outermostView
is rendered.I couldn't find a way to run the REPL using other versions of
react-pd
, which could be useful to test these scenarios.Environment: