How to import a .txt file from my source?

前端 未结 4 1198
耶瑟儿~
耶瑟儿~ 2020-12-11 14:28

I try to import a .txt file to show the text in a text box.

My code:

import React, { Component } from \'react\';
import \'./LoadMyFile.css\';
import          


        
4条回答
  •  被撕碎了的回忆
    2020-12-11 15:18

    Not wanting to use fetch as it makes me have to deal with async responses. I solved my problem like this.

    1. Created a seperate .js file and assigned my text to a variable
    2. Exported the variable
    const mytext = `test
     this is multiline text.
     more text`;
    
    export default mytext ;
    
    1. In my other component, I import the file.
    import mytext from './mytextfile.js';
    
    1. I am now free to assign it to a variable or use it anywhere in my component.
     const gotTheText = mytext;
     return ();
    

提交回复
热议问题