React - Practice useRef - Switch files PDF

화이티 ·2023년 12월 18일
0

react

목록 보기
4/11

0.Public -> create Folder pdf -> Drop 2 files pdf
Plan:
When we want to read file 1 or file 2, we input 1st or 2nd file
-> then it will be the reference for onClick
and src link when we click on will also be another reference
1. App.jsx

import React, {useRef,useState} from 'react'

const Ex08 = () => {
    const inputRef = useRef();
    const pdfRef = useRef();
    const switchFile = (num) =>{
        console.log('pdf',pdfRef.current.src);
        pdfRef.current.src = `./pdf/pdfFile${num}.pdf`
    }
    
   
    const changePdf = (src)=>{
        console.log('inputRef',inputRef.current.value);
        if(inputRef.current.value === '첫번째'){
                switchFile(1)
        }else if(inputRef.current.value === '두번째'){
                switchFile(2)
        }
    };
   



  return (
    <div>
    <input type="text" ref ={inputRef} />
    
    <button onClick={changePdf} >pdf 변경</button>
    <iframe 
    src = {switchFile}
    width= "100%"
    height="90%"
    ref = {pdfRef}
    >


    </iframe>

    </div>
  )
}

export default App
profile
열심히 공부합시다! The best is yet to come! 💜

1개의 댓글

comment-user-thumbnail
2024년 7월 15일

Nowadays PDF files are quite popular and are used for sending documents. But unfortunately it is not always convenient to work with these documents. If you want to quickly edit these documents and send them, check out https://www.pdfplatform.com/. I have been using this service for a long time and with its help working with PDF files is very fast and convenient

답글 달기