Hello Developers, In this blog, we will learn how to read text files in react-js using simple javascript
First of all, create a new app using following command
npx create-react-app
After that, we will create two text files and save them with dummy text
Now in our js file, make a new File reader
const reader = new FileReader();
Then make a function to read file
const onFileUpload = (e) => { const reader = new FileReader(); if (reader.readAsText) { reader.onload = (e) => { setData(reader.result) }; reader.readAsText(e.target.files[0]); } }
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', height: '100vh', gap: '10px' }}> <input type='file' onChange={onFileUpload} /> {data} </div>
Demo: