Skip to content

Commit

Permalink
[Fix] 使用useRef紀錄file bytes
Browse files Browse the repository at this point in the history
[Why]
因為沒有用useRef紀錄file bytes,導致有些變數變動後重新re-render
造成file bytes被初始化

[How]
使用useRef紀錄file bytes,才可取得最新狀態數值
  • Loading branch information
cabie8399 committed May 27, 2024
1 parent 47efaa2 commit da4d9b8
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React, { useState } from 'react';
import React, { useState, useRef } from 'react';
import embedImages from './utils/imgToPdf';
import './App.css';

function App() {
// eslint-disable-next-line no-unused-vars
let fileBytes;
// let classText = 'btn btn-primary disabled';
const fileBytes = useRef(null);

const [fileText, setfileText] = useState('Select your files');
const [isButtonDisabled, setButtonDisabled] = useState(true);
Expand Down Expand Up @@ -34,17 +32,16 @@ function App() {
dataBuffer.push(buffer);
}
} catch (e) {
console.log(e);
return e;
}
}
await getFile();
fileBytes = embedImages(dataBuffer);
fileBytes.current = await embedImages(dataBuffer);
convertBtnStatus(false);
};

const convert = (fileBytes) => {
const blob = new Blob([fileBytes], { type: 'application/pdf' });
const convert = () => {
const blob = new Blob([fileBytes.current], { type: 'application/pdf' });
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
const fileName = 'file.pdf';
Expand All @@ -54,7 +51,7 @@ function App() {
};

const clearAllFiles = () => {
fileBytes = null;
fileBytes.current = null;
setfileText('Select your files');
convertBtnStatus(true);
};
Expand Down

0 comments on commit da4d9b8

Please sign in to comment.