skip to Main Content
[react.js] I’m trying to refer to the content of a txt file but it if I reference to the file it will show me the path. how do I reference to the contents of the file? //the bashcode contains a bash cURL from failed API calls

[code]
import Bashcode from './Bashcode.txt';

<button onClick={() => navigator.clipboard.writeText(Bashcode)}>Naar Postman</button>
[expectations] I was actually expecting to reference to the file to get or just the file name or the contents but turned out it referenced the path to the file.

2

Answers


  1. Chosen as BEST ANSWER
    <button onClick={() => {
                          
                          fetch(Bashcode)
                          .then(r => r.text())
                          .then(text => {
                            // console.log('text decoded:', text);
                            navigator.clipboard.writeText(text);
                          });
                          }}>Copy Bash cURL</button>
    

  2. Use the File Reader module to achieve this.

    import Bashcode from './Bashcode.txt';
    
    <button onClick={() => {
      let _fr = new FileReader();
      const fileContents = _fr.readAsText(Bashcode);
      navigator.clipboard.writeText(fileContents)
    }}>Naar Postman</button>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search