-
-
Notifications
You must be signed in to change notification settings - Fork 890
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
Passing object to file prop causes React-PDF to reload a file continuously #308
Comments
Generally, creating props on the fly during render is not a good idea. If you pass an object straight into The best way to avoid it would be to create an object when you get the file data, assign it to state, and pass it as a prop in render function. If you're using hooks, you could also use For simplicity of the examples provided, I'm assuming React Hooksfunction App({ data }) {
// File object that will only get recreated when `data` changes
const file = useMemo(() => ({ data }), [data]);
return (
<Document file={file}>
// …
</Document>
);
} Legacy class componentsclass App extends React.Component {
constructor(props) {
super(props);
this.state = {
file: null,
};
}
componentDidMount() {
const { data } = this.props;
this.setState({ file: { data } });
}
componentDidUpdate(prevProps) {
const { data } = this.props;
if (data !== prevProps.data) {
// Only update state if `data` prop changes
this.setState({ file: { data } });
}
}
render() {
const { file } = this.state;
return (
<Document file={file}>
// …
</Document>
);
}
} |
Before you start - checklist
Description
Short description of the bug you encountered.
In React PDF v3, the comparison check for updating the PDF was done deeply, if the file prop was an object. This is not the case in React PDF v4, and this results in an infinite update if the object passed as a prop to filename is created in the render function...
Steps to reproduce
I can provide this later, but basically try to render a PDF with an object for the filename, and create the object in the render function.
Expected behavior
I expected it to function the same as React-PDF v3, where it would only update if the underlying value change
Additional information
I'm happy to provide a pull-request, if this is an unintended side-effect, as well as a test case demonstrating this.
Environment
The text was updated successfully, but these errors were encountered: