Skip to content

Commit

Permalink
fix no args update call
Browse files Browse the repository at this point in the history
  • Loading branch information
jeetiss committed Jun 15, 2023
1 parent ce7474f commit 4ac19e7
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/renderer/src/dom/usePDF.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ import { useState, useRef, useEffect, useCallback } from 'react';

import { pdf } from '../index';

const useCommittedRef = value => {
const ref = useRef();

useEffect(() => {
ref.current = value;
});

return ref;
};

export const usePDF = ({ document } = {}) => {
const pdfInstance = useRef(null);
const committedDocument = useCommittedRef(document);

const [state, setState] = useState({
url: null,
Expand Down Expand Up @@ -65,8 +76,15 @@ export const usePDF = ({ document } = {}) => {
};
}, [state.url]);

const update = useCallback(newDoc => {
pdfInstance.current.updateContainer(newDoc);
// make update function stable
const update = useCallback((...args) => {
if (args.length === 0) {
// TODO: add warning that no args update would be removed in next versions
pdfInstance.current.updateContainer(committedDocument.current);
} else {
const [newDoc] = args;
pdfInstance.current.updateContainer(newDoc);
}
}, []);

return [state, update];
Expand Down

0 comments on commit 4ac19e7

Please sign in to comment.